笔记:在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个人主页-哔哩哔哩视频

相关推荐
浅念-几秒前
C语言——单链表
c语言·开发语言·数据结构·经验分享·笔记·算法·leetcode
flashier22 分钟前
ESP32学习笔记_WiFi(2)——TCP/UDP
笔记·学习·tcp/ip·wifi·esp32
峰顶听歌的鲸鱼2 小时前
Kubernetes核心概述
运维·笔记·云原生·容器·kubernetes·云计算
代码游侠2 小时前
学习笔记——GPIO按键与中断系统
c语言·开发语言·arm开发·笔记·嵌入式硬件·学习·重构
浅念-2 小时前
C++第一课
开发语言·c++·经验分享·笔记·学习·算法
蓝田生玉1232 小时前
PLUTO论文阅读笔记
论文阅读·笔记
1379号监听员_3 小时前
PID学习笔记
笔记·学习
执笔论英雄3 小时前
【大模型推理】VLLM 引擎使用
wpf·vllm
洁宝趴趴3 小时前
阅读笔记How to Set the Batch Size for Large-ScalePre-training?
人工智能·笔记·深度学习
LateFrames3 小时前
动画性能比对:WPF / WinUI3 / WebView2
wpf·webview·用户体验·winui3