WPF 附加事件
路由事件的宿主是那些有 UI 显示功能的界面元素,而附加事件是那些没有 UI 显示功能的元素,其本质还是路由事件,只是路由事件的宿主不一样。附加事件只是路由事件的一种用法而已。
XAML:
<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 类:
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 代码:
/// <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
类,所以,没有 AddHandler
和 RemoveHandler
两个方法,也没有 RiaseEvent
方法。
根据微软规定,可以升级一下:
Student 类:
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:
/// <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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了