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

相关推荐
東隅已逝,桑榆非晚18 小时前
字符函数和字符串函数
c语言·笔记
枫叶林FYL18 小时前
项目九:异步高性能爬虫与数据采集中枢 —— 基于 Crawl<sub>4</sub>AI 与 Playwright 的现代化数据采集平台 项目总览
爬虫·python·深度学习·wpf
Upsy-Daisy18 小时前
AI Agent 项目学习笔记(七):RAG 高级扩展——过滤检索、PgVector 与云知识库
人工智能·笔记·学习
智者知已应修善业19 小时前
【51单片机LED闪烁10次数码管显示0-9】2023-12-14
c++·经验分享·笔记·算法·51单片机
智者知已应修善业19 小时前
【51单片机2按键控制1个敞亮LED灯闪烁和熄灭】2023-11-3
c++·经验分享·笔记·算法·51单片机
w20180021 小时前
二年级下册语文看图写话作文:蛋壳的奇妙之旅
笔记
daanpdf21 小时前
初三中考英语作文模板万能句型及范文大全电子版
笔记
nnsix21 小时前
设计模式 - 建造者模式 笔记
笔记·设计模式·建造者模式
穗余1 天前
2026 AI x Web3 School共学营笔记-Day1
人工智能·笔记·web3
sheeta19981 天前
LeetCode 每日一题笔记 日期:2026.05.20 题目:2657. 找到前缀公共数组
笔记·算法·leetcode