Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为鼠标点击设置不透明的文本块-使之成为家长面板';s事件不';t火_C#_Wpf_Events - Fatal编程技术网

C# 为鼠标点击设置不透明的文本块-使之成为家长面板';s事件不';t火

C# 为鼠标点击设置不透明的文本块-使之成为家长面板';s事件不';t火,c#,wpf,events,C#,Wpf,Events,那么如何使textblock(或其他)框架元素具有单击事件呢。但是,如果触发了该单击事件,则不会触发父级(网格)的单击事件。-如果未单击文本框/任何内容,则应触发父级的click事件 以下是一个简单的示例: using System; using System.Windows; using System.Windows.Controls; using System.Windows.Input; namespace testit { /// <summary> ///

那么如何使textblock(或其他)框架元素具有单击事件呢。但是,如果触发了该单击事件,则不会触发父级(网格)的单击事件。-如果未单击文本框/任何内容,则应触发父级的click事件

以下是一个简单的示例:

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace testit
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow() {
            InitializeComponent();
            Grid mainGrid = (Grid)FindName("MainGrid");
            mainGrid.MouseLeftButtonDown += ClickInNowhere;
            TextBlock tb = new TextBlock {
                Text = "hello\nworld"
            };
            tb.MaxWidth = 100;
            tb.MaxHeight = 100;
            mainGrid.Children.Add(tb);
            tb.MouseLeftButtonDown += ClickOnBox;
        }

        private void ClickInNowhere(object o, MouseButtonEventArgs args) {
            tb.Text = "hello\nworld";
        }

        private void ClickOnBox(object o, MouseButtonEventArgs args) {
            tb.Text += "a";
            //do something so that the "MouseLeftButtonDown" of the main window doesn't fire
        }
    }
}
使用系统;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Input;
名称空间测试
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
公共主窗口(){
初始化组件();
网格主网格=(网格)FindName(“主网格”);
mainGrid.MouseLeftButtonDown+=单击Innowhere;
TextBlock tb=新的TextBlock{
Text=“你好\n世界”
};
tb.MaxWidth=100;
tb.MaxHeight=100;
主网格.Children.Add(tb);
tb.MouseLeftButtonDown+=点击框;
}
私有void ClickInNowhere(对象o、鼠标按钮ventargs args){
tb.Text=“hello\nworld”;
}
私有无效点击框(对象o,鼠标按钮Ventargs参数){
tb.Text+=“a”;
//做点什么,这样主窗口的“MouseLeftButtonDown”就不会触发
}
}
}
使用xml格式:

<Window x:Class="testit.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:testit"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid x:Name="MainGrid" Background="#FFC8C8C8" />
</Window>

请注意,网格有一个背景,使其对鼠标事件作出反应。应该有两种情况:

  • 单击文本框:框中的文本附加“a”
  • 单击框外:文本框中的文本将重置为“hello\nworld”
这显然不会发生:当通过调试器运行时,可以看到两个事件都被触发


那么,如何使文本块对鼠标事件不透明?

您需要说,该事件已被处理:

private void ClickInNowhere(object o, MouseButtonEventArgs args) {
        tb.Text = "hello\nworld";
        args.Handled = true;
}
MouseButtonEventArgs从RoutedEventArgs派生,RoutedEventArgs具有Handled属性,在这里您可以阅读更多有关它的信息:


另外,我建议阅读有关事件冒泡的内容:

您需要做的是将鼠标事件标记为已处理

private void ClickOnBox(object o, MouseButtonEventArgs args) {
        tb.Text += "a";
        //do something so that the "MouseLeftButtonDown" of the main window doesn't fire
        args.Handled = true;
    }

MouseButtonEventArgs中,将Handle属性设置为true。 因为它是一个冒泡事件,并且通过将Handle设置为true,它将不会在父级中处理

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace testit
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Grid mainGrid = (Grid)FindName("MainGrid");
            mainGrid.MouseLeftButtonDown += ClickInNowhere;
            TextBlock tb = new TextBlock
            {
                Text = "hello\nworld"
            };
            tb.MaxWidth = 100;
            tb.MaxHeight = 100;
            mainGrid.Children.Add(tb);
            tb.MouseLeftButtonDown += ClickOnBox;
        }

        private void ClickInNowhere(object o, MouseButtonEventArgs args)
        {
            tb.Text = "hello\nworld";
        }

        private void ClickOnBox(object o, MouseButtonEventArgs args)
        {
            tb.Text += "a";
            args.Handled = true;
        }
    }
}
使用系统;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Input;
名称空间测试
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
网格主网格=(网格)FindName(“主网格”);
mainGrid.MouseLeftButtonDown+=单击Innowhere;
TextBlock tb=新的TextBlock
{
Text=“你好\n世界”
};
tb.MaxWidth=100;
tb.MaxHeight=100;
主网格.Children.Add(tb);
tb.MouseLeftButtonDown+=点击框;
}
私有void ClickInNowhere(对象o、鼠标按钮ventargs args)
{
tb.Text=“hello\nworld”;
}
私有无效点击框(对象o,鼠标按钮Ventargs参数)
{
tb.Text+=“a”;
args.Handled=true;
}
}
}