Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.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# 需要帮助理解WPF中的事件冒泡代码吗_C#_Wpf_Events_Event Bubbling - Fatal编程技术网

C# 需要帮助理解WPF中的事件冒泡代码吗

C# 需要帮助理解WPF中的事件冒泡代码吗,c#,wpf,events,event-bubbling,C#,Wpf,Events,Event Bubbling,由于事件冒泡,下面的代码本应在单击按钮时将表单中的所有按钮变为绿色,但在我的Visual studio 2008计算机上,它仅将单击的按钮变为绿色,请帮助解决此问题 XAML代码(window1.XAML): CS代码(window1.xaml.CS) 使用系统; 使用System.Collections.Generic; 使用System.Linq; 使用系统文本; 使用System.Windows; 使用System.Windows.Controls; 使用System.Window

由于事件冒泡,下面的代码本应在单击按钮时将表单中的所有按钮变为绿色,但在我的Visual studio 2008计算机上,它仅将单击的按钮变为绿色,请帮助解决此问题

XAML代码(window1.XAML):


CS代码(window1.xaml.CS)

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
使用系统诊断;
命名空间事件路由
{
/// 
///Window1.xaml的交互逻辑
/// 
公共部分类Window1:Window
{
公共窗口1()
{
初始化组件();
this.MouseEnter+=MouseEnterHandler;
myBorder.MouseEnter+=MouseEnterHandler;
myPanel.MouseEnter+=MouseEnterHandler;
myEllipse.MouseEnter+=MouseEnterHandler;
myRectangle.MouseEnter+=MouseEnterHandler;
this.MouseDown+=MouseDownHandler;
myBorder.MouseDown+=MouseDownHandler;
myPanel.MouseDown+=MouseDownHandler;
myEllipse.MouseDown+=MouseDownHandler;
myRectangle.MouseDown+=MouseDownHandler;

对于(int i=1;i,因为事件处理程序btn_Click(object sender,MouseEventArgs e)只发送您单击的按钮,所以不能单独使用sender对象将文本更改为绿色


您应该执行类似于
foreach(myPanel.Children中的按钮btn)
的操作,在其中循环所有按钮并更改循环中的颜色。

因为事件处理程序btn\u单击(对象发送器,MouseEventArgs e)正在仅发送您单击的按钮您不能单独使用发件人对象将文本更改为绿色


您应该执行类似于
foreach(myPanel.Children中的按钮btn)
的操作,在其中循环所有按钮并更改循环中的颜色。

路由事件在视觉树中冒泡,直到得到处理。请使用XAML尝试此代码

    public MainWindow()
    {
        InitializeComponent();
        myEllipse.AddHandler(UIElement.MouseDownEvent, new RoutedEventHandler(OnMouseDown));
        myPanel.AddHandler(UIElement.MouseDownEvent, new RoutedEventHandler(OnMouseDown));
        myBorder.AddHandler(UIElement.MouseDownEvent, new RoutedEventHandler(OnMouseDown));
    }

    void OnMouseDown(object sender, RoutedEventArgs e)
    {
        UIElement uiElement = sender as UIElement;
        Debug.WriteLine(uiElement.GetType().ToString());
        e.Handled = true;
    }

如果注释掉e.Handled=true行,事件将冒泡到父元素。这对您有好处。

路由事件会冒泡到可视化树中,直到它们得到处理。请使用XAML尝试此代码

    public MainWindow()
    {
        InitializeComponent();
        myEllipse.AddHandler(UIElement.MouseDownEvent, new RoutedEventHandler(OnMouseDown));
        myPanel.AddHandler(UIElement.MouseDownEvent, new RoutedEventHandler(OnMouseDown));
        myBorder.AddHandler(UIElement.MouseDownEvent, new RoutedEventHandler(OnMouseDown));
    }

    void OnMouseDown(object sender, RoutedEventArgs e)
    {
        UIElement uiElement = sender as UIElement;
        Debug.WriteLine(uiElement.GetType().ToString());
        e.Handled = true;
    }

如果注释掉e.Handled=true行,事件将冒泡到父元素。这对您有好处。

如果我理解您想要的是从根(面板)到每个子元素(按钮)的隧道事件.Routed tunnel事件不这样做,它们只从根元素到源元素,而不是到同级元素。请参阅,并提供一个很好的示例。

如果我了解您想要的是从根(面板)到每个子元素(按钮)的隧道事件.Routed tunnel事件不会这样做,它们只会从根元素到源元素,而不会到同级元素。请参阅,并提供一个很好的示例。

但是该事件正在添加到myPanel(stackPanel),它保留了所有按钮,因此应该应用到所有按钮?但是该事件正在添加到myPanel(stackPanel),它保留了所有按钮,所以它应该应用于所有按钮?
    public MainWindow()
    {
        InitializeComponent();
        myEllipse.AddHandler(UIElement.MouseDownEvent, new RoutedEventHandler(OnMouseDown));
        myPanel.AddHandler(UIElement.MouseDownEvent, new RoutedEventHandler(OnMouseDown));
        myBorder.AddHandler(UIElement.MouseDownEvent, new RoutedEventHandler(OnMouseDown));
    }

    void OnMouseDown(object sender, RoutedEventArgs e)
    {
        UIElement uiElement = sender as UIElement;
        Debug.WriteLine(uiElement.GetType().ToString());
        e.Handled = true;
    }