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

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

二、实现

应用程序级全局事件

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

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

控件级全局事件

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

XML 复制代码
            <ItemsControl ItemsSource="{h:GetStudents}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Button Content="{Binding Name}"/>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>

主要实现方式有两种:

1> 在Xaml中实现
XML 复制代码
            <ItemsControl x:Name="ic_event" ButtonBase.Click="ItemsControl_Button_Click" ItemsSource="{h:GetStudents}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Button Content="{Binding Name}"/>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
cs 复制代码
        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>在代码中实现
XML 复制代码
//在代码中注册控件级全局事件
this.ic_event.AddHandler(Button.ClickEvent, new RoutedEventHandler(ItemsControl_Button_Click));

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

总结

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

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

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

XML 复制代码
            <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个人主页-哔哩哔哩视频

相关推荐
hssfscv2 小时前
Javaweb 学习笔记——html+css
前端·笔记·学习
Dream Algorithm3 小时前
自古英雄多寂寥
笔记
yuhaiqun19894 小时前
Typora 技能进阶:从会写 Markdown 到玩转配置 + 插件高效学习笔记
经验分享·笔记·python·学习·学习方法·ai编程·markdown
apcipot_rain4 小时前
汇编语言与逆向分析 一轮复习笔记
汇编·笔记·逆向
Lv11770084 小时前
Visual Studio中的多态
ide·笔记·c#·visual studio
HollowKnightZ4 小时前
论文阅读笔记:Class-Incremental Learning: A Survey
论文阅读·笔记
AA陈超5 小时前
枚举类 `ETriggerEvent`
开发语言·c++·笔记·学习·ue5
代码游侠5 小时前
学习笔记——IPC(进程间通信)
linux·运维·网络·笔记·学习·算法
apcipot_rain6 小时前
汇编语言程序设计 从0到1实战笔记
笔记
周小码6 小时前
我用一个周末,写了一个“反内卷“的极简笔记工具
笔记