笔记:在WPF中如何控件级全局事件和应用程序级全局事件

一、目的:在WPF中如何控件级全局事件和应用程序级全局事件


二、实现

应用程序级全局事件

//注册应用程序级全局事件
EventManager.RegisterClassHandler(typeof(Button), Button.ClickEvent, new RoutedEventHandler(ic_event_Click));

如上代码既会注册全局Button的Click事件,在任意位置点击Button既会触发注册的事件,但这种方式作用范围过大,对于有些业务来说这种方式会照成资源和性能上的浪费,本文将主要介绍空间级别的全局事件,这对于某功能来说既可以实现特殊的业务,也可以有效的控制性能。

控件级全局事件

比如有一个ItemsControl控件,绑定了一个集合,集合里面有很多Button操作的按钮,我们想要注册这些按钮的Click操作,这时需要用到控件级别的全局Button的Click事件。代码如下:

            <ItemsControl ItemsSource="{h:GetStudents}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Button Content="{Binding Name}"/>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>

 主要实现方式有两种:

1> 在Xaml中实现
            <ItemsControl x:Name="ic_event" ButtonBase.Click="ItemsControl_Button_Click" ItemsSource="{h:GetStudents}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Button Content="{Binding Name}"/>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        private void ItemsControl_Button_Click(object sender, RoutedEventArgs e)
        {
            if (e.OriginalSource is Button button)
                MessageBox.Show(button.Content?.ToString());
        }

 可以看到直接在ItemsControl上注册Button.Click事件即可实现该效果,此时当点击控件中任意Button时会触发该事件

2>在代码中实现
//在代码中注册控件级全局事件
this.ic_event.AddHandler(Button.ClickEvent, new RoutedEventHandler(ItemsControl_Button_Click));

在代码中实现也比较简单,只需应用AddHandler方法注册一个Button.ClickEvent即可实现

总结

1、通过这种方式可以解决部分特殊业务,如上述示例中,可以注册控件内所有按钮点击事件而不需要对每个按钮单独做处理;

2、不仅仅局限Button.Click事件,任何路由事件和附加事件均可通过上述方式实现;

3、可以实现没有暴露出来的鼠标,键盘,触摸板等应用附加事件定义的功能进行注册,如:

            <ItemsControl x:Name="ic_event" Mouse.GotMouseCapture="ic_event_GotMouseCapture" ItemsSource="{h:GetStudents}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Button Content="{Binding Name}"/>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>

 ItmsControl没有暴露GotMouseCapture事件,可以通过Mouse.GotMouseCapture去注册该事件

三、效果

需要了解的知识点 

EventManager Class (System.Windows) | Microsoft Learn 

EventManager.RegisterClassHandler 方法 (System.Windows) | Microsoft Learn 

ButtonBase.Click 事件 (System.Windows.Controls.Primitives) | Microsoft Learn 

UIElement.AddHandler 方法 (System.Windows) | Microsoft Learn 

UIElement.RemoveHandler(RoutedEvent, Delegate) Method (System.Windows) | Microsoft Learn 

Mouse 类 (System.Windows.Input) | Microsoft Learn 

Keyboard 类 (System.Windows.Input) | Microsoft Learn 

System.Windows.Controls 命名空间 | Microsoft Learn

控件库 - WPF .NET Framework | Microsoft Learn

WPF 介绍 | Microsoft Learn

XAML概述 - WPF .NET | Microsoft Learn

Windows Presentation Foundation 简介 - WPF .NET | Microsoft Learn

使用 Visual Studio 创建新应用教程 - WPF .NET | Microsoft Learn

源码地址

GitHub - HeBianGu/WPF-ControlDemo: 示例

GitHub - HeBianGu/WPF-ControlBase: Wpf封装的自定义控件资源库

GitHub - HeBianGu/WPF-Control: WPF轻量控件和皮肤库

了解更多

适用于 .NET 8 的 WPF 的新增功能 - WPF .NET | Microsoft Learn

适用于 .NET 7 的 WPF 的新增功能 - WPF .NET | Microsoft Learn

System.Windows.Controls 命名空间 | Microsoft Learn

欢迎使用 Expression Blend | Microsoft Learn

https://github.com/HeBianGu

HeBianGu的个人空间-HeBianGu个人主页-哔哩哔哩视频

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值