WPF 附加事件

路由事件的宿主是那些有 UI 显示功能的界面元素,而附加事件是那些没有 UI 显示功能的元素,其本质还是路由事件,只是路由事件的宿主不一样。附加事件只是路由事件的一种用法而已。

XAML:

1
2
3
4
5
6
7
8
9
10
11
12
<Window x:Class="WpfApp1.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:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="300" Width="300">
<Grid x:Name="gridMain">
<Button x:Name="button1" Content="OK" Width="80" Height="80" Click="Button1_Click"/>
</Grid>
</Window>

▲ 只做了一个按钮触发一下

附加事件宿主,Student 类:

1
2
3
4
5
6
7
8
public class Student
{
public static readonly RoutedEvent NameChangedEvent = EventManager.RegisterRoutedEvent
("NameChanged", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(Student));

public int Id { get; set; }
public string Name { get; set; }
}

C# behind 代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();

gridMain.AddHandler(Student.NameChangedEvent, new RoutedEventHandler(StudentNameChangedHandler));
}

private void Button1_Click(object sender, RoutedEventArgs e)
{
Student stu = new Student() { Id=10101, Name = "Tim" };
stu.Name = "Tom";
// 准备事件消息,并发送路由事件
RoutedEventArgs args = new RoutedEventArgs(Student.NameChangedEvent, stu);
button1.RaiseEvent(args); // 借用下 RaiseEvent 函数,借用 gridMain 的也可以
}

// Grid 捕捉到 NameChangedEvent 后的处理器
private void StudentNameChangedHandler(object sender, RoutedEventArgs e)
{
MessageBox.Show((e.OriginalSource as Student).Id.ToString());
}
}

理论上,上面已经算一个附加事件了。

▲ 点击按钮,弹出学号

因为 Student 不是继承 UIElement 类,所以,没有 AddHandlerRemoveHandler 两个方法,也没有哦 RiaseEvent 方法。

根据微软规定,可以升级一下:

Student 类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
public class Student
{
// 声明并定义路由事件
public static readonly RoutedEvent NameChangedEvent = EventManager.RegisterRoutedEvent
("NameChanged", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(Student));

/// <summary>
/// 为界面元素增加路由事件监听
/// </summary>
/// <param name="d">事件的监听者</param>
/// <param name="h">事件处理器委托类型</param>
public static void AddNameChangedHandler(DependencyObject d, RoutedEventHandler h)
{
UIElement uIElement = d as UIElement;
if (uIElement != null)
{
uIElement.AddHandler(Student.NameChangedEvent, h);
}
}

/// <summary>
/// 移除侦听
/// </summary>
/// <param name="d">事件的监听者</param>
/// <param name="h">事件处理器委托类型</param>
public static void RemoveNameChangedHandler(DependencyObject d, RoutedEventHandler h)
{
UIElement uIElement = d as UIElement;
if (uIElement != null)
{
uIElement.RemoveHandler(Student.NameChangedEvent, h);
}
}

public int Id { get; set; }
public string Name { get; set; }
}

C# Behind:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();

// 这个地方改一下
Student.AddNameChangedHandler(gridMain, new RoutedEventHandler(StudentNameChangedHandler));
}

private void Button1_Click(object sender, RoutedEventArgs e)
{
Student stu = new Student() { Id = 10101, Name = "Tim" };
stu.Name = "Tom";
// 准备事件消息,并发送路由事件
RoutedEventArgs args = new RoutedEventArgs(Student.NameChangedEvent, stu);
button1.RaiseEvent(args);
}

// Grid 捕捉到 NameChangedEvent 后的处理器
private void StudentNameChangedHandler(object sender, RoutedEventArgs e)
{
MessageBox.Show((e.OriginalSource as Student).Id.ToString());
}
}

附加事件,只是路由事件的一种用法,而非一个新概念。




参考: 《WPF 深入浅出》 - P171

感谢支持!