WPF 自定义路由事件

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

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

▲ 运行结果

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"
        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 代码:

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);
        }
    }
}

自定义路由事件:


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

posted @   double64  阅读(120)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示