需要帮助了解 WPF 中的事件冒泡代码
本文关键字:事件 代码 帮助 了解 WPF | 更新日期: 2023-09-27 18:35:11
由于事件冒泡,以下代码应该在其中一个按钮单击时将表单中存在的所有按钮转换为绿色,但是在Visual studio 2008上的计算机上,它仅将单击的按钮变为绿色,您能帮助找出问题吗?
XAML 代码 (window1.xaml):
<Window x:Class="EventRouting.Window1" Title="Event Routing" Height="300" Width="300"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Border Margin="15" BorderBrush="Blue" BorderThickness="5" Padding="15"
CornerRadius="12" x:Name="myBorder" Background="Transparent">
<StackPanel x:Name="myPanel" Background="Transparent">
<Ellipse x:Name="myEllipse" Margin="3" Fill="Green" Height="40" />
<Rectangle x:Name="myRectangle" Margin="3" Fill="Cyan" Height="40" RadiusX="10" RadiusY="10" />
</StackPanel>
CS 代码 (window1.xaml.cs)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;
namespace EventRouting
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
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;
for (int i = 1; i <= 5; ++i)
{
Button btn = new Button();
btn.Content = "Button " + i;
myPanel.Children.Add(btn);
//btn.Click += new RoutedEventHandler(btn_Click);
}
myPanel.AddHandler(Button.ClickEvent, new RoutedEventHandler(btn_Click));
}
void btn_Click(object sender, RoutedEventArgs e)
{
Button btn = (Button) e.Source;
btn.Background = Brushes.Green;
}
void MouseEnterHandler(object sender, MouseEventArgs e)
{
Debug.WriteLine("MouseEnter: " + sender);
}
void MouseDownHandler(object sender, MouseButtonEventArgs e)
{
Debug.WriteLine("MouseDown: " + sender);
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.Handle = true 行,事件将冒泡到父元素。 这是一个很好的链接。
如果我理解你想要的是从根(面板)到每个子级(按钮)的隧道事件。路由隧道事件不会这样做,它们只从根元素遍历到源元素,而不是遍历到同级元素。请参阅 WPF 中的路由事件概述,并对此进行了很好的说明。
因为事件处理程序btn_Click(对象发送器,MouseEventArgs e)正在发送您单击的按钮,所以您不能单独使用发送方对象将文本更改为绿色。
你应该做一些类似foreach (Button btn in myPanel.Children)
的事情,你循环遍历所有按钮并更改循环中的颜色。