WPF 自定义路由事件

路由事件和依赖属性一样,都是注册到了 Hastable 里面去了。

路由事件的好处:不再受直接事件那种,需要一层一层往外传递的麻烦,能直接”飞来飞去”。

▲ 运行结果

XAML 代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<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"
x:Name="window_1"
local:TimeButton.ReportTime="ReportTimeHandler"
Title="MainWindow" Height="350" Width="420">
<Grid x:Name="grid_1" local:TimeButton.ReportTime="ReportTimeHandler">
<Grid x:Name="grid_2" local:TimeButton.ReportTime="ReportTimeHandler">
<Grid x:Name="grid_3" local:TimeButton.ReportTime="ReportTimeHandler">
<StackPanel x:Name="stackPanel_1" Orientation="Vertical" local:TimeButton.ReportTime="ReportTimeHandler">
<ScrollViewer x:Name="scrollView_1" VerticalScrollBarVisibility="Auto" local:TimeButton.ReportTime="ReportTimeHandler">
<ListBox x:Name="listBox" BorderBrush="Black" BorderThickness="1" local:TimeButton.ReportTime="ReportTimeHandler" Margin="5" Height="240"/>
</ScrollViewer>
<local:TimeButton x:Name="timeButton" Width="80" Height="40" Content="报时" Margin="10" local:TimeButton.ReportTime="ReportTimeHandler"/>
</StackPanel>
</Grid>
</Grid>
</Grid>
</Window>

C# Behind 代码:

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


private void ReportTimeHandler(object sender, ReportTimeEventArgs e)
{
FrameworkElement element = sender as FrameworkElement;
string timeStr = e.ClickTime.ToLongTimeString();
string content = $"{timeStr} 到达 {element.Name}";
listBox.Items.Add(content);
}
}
}

自定义路由事件:

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
38

namespace WpfApp1
{
// 用于承载事件消息的时间参数
class ReportTimeEventArgs : RoutedEventArgs
{
public ReportTimeEventArgs(RoutedEvent routedEvent, object source) :
base(routedEvent, source)
{
}

public DateTime ClickTime { get; set; }
}

class TimeButton : Button
{
// 声明和注册路由事件
public static readonly RoutedEvent ReportTimeEvent = EventManager.RegisterRoutedEvent
("ReportTime", RoutingStrategy.Bubble, typeof(EventHandler<ReportTimeEventArgs>), typeof(TimeButton));

//CLR 事件包装器
public event RoutedEventHandler ReportTime
{
add { AddHandler(ReportTimeEvent, value); }
remove { RemoveHandler(ReportTimeEvent, value); }
}

// 激发路由事件,借用 Click 事件的激发方法
protected override void OnClick()
{
base.OnClick(); // 保证 Button 原有的功能正常使用, CLick 事件能被激发

ReportTimeEventArgs args = new ReportTimeEventArgs(ReportTimeEvent, this);
args.ClickTime = DateTime.Now;
RaiseEvent(args);
}
}
}




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

感谢支持!