WPF:自定义路由事件的实现
路由事件通过EventManager,RegisterRoutedEvent方法注册,通过AddHandler和RemoveHandler来关联和解除关联的事件处理函数;通过RaiseEvent方法来触发事件;通过传统的CLR事件来封装后供用户使用。
如何实现自定义路由事件,可以参考MSDN官网上的文档:如何:创建自定义路由事件
下面的这个demo参考自<葵花宝典--WPF自学手册>。
1、MainWindow.xaml
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:WpfApplication1"
Title="MainWindow" Height="518" Width="525"
local:MySimpleButton.CustomClick="InsertList"
Loaded="Window_Loaded">
<Grid Name="grid1" local:MySimpleButton.CustomClick="InsertList">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition> </Grid.RowDefinitions>
<local:MySimpleButton x:Name="simpleBtn" CustomClick="InsertList" >
MySimpleBtn
</local:MySimpleButton>
<ListBox Name="lstMsg" Grid.Row="1"></ListBox>
<CheckBox Grid.Row="2" Name="chkHandle">Handle first event</CheckBox>
<Button Grid.Row="3" Click="cmdClear_Click">Clear list</Button>
</Grid> </Window>
在xaml文件中,完成页面的元素布局之后,给几个元素添加了事件处理函数。
(1)给Window添加了Loaded事件的处理函数,还添加了MySimpleButton的CustomClick事件的类事件处理函数
local:MySimpleButton.CustomClick="InsertList"
Loaded="Window_Loaded"
(2)给Grid同样添加了MySimpleButton的类事件处理函数
(3)给MySimpleButton元素添加了CustomClick事件的实例事件处理函数
CustomClick="InsertList"
(4)给Button元素添加了Click事件处理函数
Click="cmdClear_Click"
2、MySimpleButton.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows; namespace WpfApplication1
{
//继承Button类,自定义一个名为MySimpleButton的Button
public class MySimpleButton:Button
{
//———————————类事件处理函数————————————
static MySimpleButton()
{
//为路由事件CustomClickEvent注册一个类事件处理函数
//类事件处理函数的优先权高于实例事件处理函数
EventManager.RegisterClassHandler(typeof(MySimpleButton), CustomClickEvent, new RoutedEventHandler(CustomClickClassHandler), false);
}
//创建一个名为CustomClickClassHandler的类事件处理函数
//为了通知外部窗口,把路由事件的信息输出,需要添加一个普通的CLR事件ClassHandlerProcessed
public event EventHandler ClassHandlerProcessed;
public static void CustomClickClassHandler(object sender, RoutedEventArgs e)
{
MySimpleButton simpleBtn = sender as MySimpleButton;
EventArgs args = new EventArgs();
simpleBtn.ClassHandlerProcessed(simpleBtn, args);
} //———————————实例事件处理函数————————————
//创建和注册一个名为CustomClickEvent的路由事件,路由策略为Bubble
public static readonly RoutedEvent CustomClickEvent = EventManager.RegisterRoutedEvent("CustomClick", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(MySimpleButton));
//给路由事件添加一个CLR事件包装器
public event RoutedEventHandler CustomClick
{
add
{
AddHandler(CustomClickEvent, value);
}
remove
{
RemoveHandler(CustomClickEvent, value);
}
}
//RaiseEvent()触发CustomClickEvent事件
protected override void OnClick()
{
RaiseCustomClickEvent();
}
void RaiseCustomClickEvent()
{
RoutedEventArgs newEventArgs = new RoutedEventArgs(MySimpleButton.CustomClickEvent);
RaiseEvent(newEventArgs);
} }
}
这个是自定义的一个按钮类,在里面创建了自定义的路由事件。
3、MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes; namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
//MySimpleButton的类事件处理函数处理过,window就能得到通知
this.simpleBtn.ClassHandlerProcessed += new EventHandler(simpleBtn_RaisedClass);
}
protected int eventCount = ;
//CusomClick的事件处理函数
private void InsertList(object sender, RoutedEventArgs e)
{
eventCount++;
string msg = "#" + eventCount.ToString() + ":\r\n" + "InsertList\r\n" + "Sender:" + sender.ToString() + "\r\n Source:" + e.Source+"\r\n"+"Original Source:"+e.OriginalSource;
lstMsg.Items.Add(msg);
//CheckBox选中状态表示路由事件是否已处理,若已处理,则不在传递
e.Handled = (bool)chkHandle.IsChecked;
}
//类事件处理函数已经完成,打印信息
private void simpleBtn_RaisedClass(object sender, EventArgs e)
{
eventCount++;
string msg = "#" + eventCount.ToString() + ":\r\n WindowClassHandler\r\nSender:" + sender.ToString();
lstMsg.Items.Add(msg);
}
//Clear列表内容
private void cmdClear_Click(object sender, RoutedEventArgs e)
{
eventCount = ;
lstMsg.Items.Clear();
}
//在window的Load事件中给Grid另外添加一个名为ProcessHandlersToo的路由事件处理函数
//通过这种方式添加,即使路由事件被标记"已处理",处理函数仍然会执行
private void Window_Loaded(object sender, RoutedEventArgs e)
{
grid1.AddHandler(MySimpleButton.CustomClickEvent, new RoutedEventHandler(ProcessHandlerToo), true);
} private void ProcessHandlerToo(object sender, RoutedEventArgs e)
{
eventCount++;
string msg = "#" + eventCount.ToString() + ":\r\n" + "InsertList\r\n" + "Sender:" + sender.ToString() + "\r\n Source:" + e.Source + "\r\n" + "Original Source:" + e.OriginalSource;
lstMsg.Items.Add(msg); }
}
}
上面是路由事件的具体处理。
4、运行效果

从上面的运行效果可以看到,
(1)CheckBox未选中
路由事件在传递时,首先被类事件处理函数处理,然后沿着视觉树向上传递(MySimpleButton-->Grid-->Window),依次被添加了实例事件处理函数的元素进行事件处理。在传到Grid元素时,先进行InserList处理,再进行ProcessHandlerToo处理,这两个事件处理函数是用不同方式添加的,执行顺序不同。
(2)CheckBox选中
选中了CheckBox,则路由事件传递到MySimpleButton元素并进行处理后,被标记成"已处理",则之后不再向上传递,Grid和Window元素不再执行InsertList,但是Grid中的处理函数ProcessHandlerToo仍然会执行,这是两种事件添加方式不同的地方。
WPF:自定义路由事件的实现的更多相关文章
- WPF自定义路由事件(二)
WPF中的路由事件 as U know,和以前Windows消息事件区别不再多讲,这篇博文中,将首先回顾下WPF内置的路由事件的用法,然后在此基础上自定义一个路由事件. 1.WPF内置路由事件 WPF ...
- 细说WPF自定义路由事件
WPF中的路由事件 as U know,和以前Windows消息事件区别不再多讲,这篇博文中,将首先回顾下WPF内置的路由事件的用法,然后在此基础上自定义一个路由事件. 1.WPF内置路由事件 W ...
- WPF 自定义路由事件
如何:创建自定义路由事件 首先自定义事件支持事件路由,需要使用 RegisterRoutedEvent 方法注册 RoutedEvent C#语法 public static RoutedEvent ...
- Wpf自定义路由事件
创建自定义路由事件大体可以分为三个步骤: ①声明并注册路由事件. ②为路由事件添加CLR事件包装. ③创建可以激发路由事件的方法. 以ButtonBase类中代码为例展示这3个步骤: public a ...
- WPF自定义路由事件(一)
首先自定义事件支持事件路由,需要使用 RegisterRoutedEvent 方法注册 RoutedEvent C#语法 public static RoutedEvent RegisterRoute ...
- WPF 自定义路由事件 与 附加路由事件
为student添加附件事件
- WPF自学入门(四)WPF路由事件之自定义路由事件
在上一遍博文中写到了内置路由事件,其实除了内置的路由事件,我们也可以进行自定义路由事件.接下来我们一起来看一下WPF中的自定义路由事件怎么进行创建吧. 创建自定义路由事件分为3个步骤: 1.声明并注册 ...
- WPF路由事件三:自定义路由事件
与依赖项属性类似,WPF也为路由事件提供了WPF事件系统这一组成.为一个类型添加一个路由事件的方式与为类型添加依赖项属性的方法类似,添加一个自定义路由事件的步骤: 一.声明路由事件变量并注册:定义只读 ...
- WPF的路由事件、冒泡事件、隧道事件(预览事件)
本文摘要: 1:什么是路由事件: 2:中断事件路由: 3:自定义路由事件: 4:为什么需要自定义路由事件: 5:什么是冒泡事件和预览事件(隧道事件): 1:什么是路由事件 WPF中的事件为路由事件,所 ...
随机推荐
- Git工具:Widows下的使用(提交到Github)
2016年12月9日17:07:07 Git工作原理 http://deweixu.me/2016/11/05/how-git-works/ 2016年12月1日14:25:23 ---------- ...
- 【Alpha版本】测试文档
App测试点 UI测试 测试各界面控件布局.总体色调.风格是否能够给用户良好的使用感. 文字是否正确,图文符合,文字与图片的组合是否够美观. 操作是否友好,是否易于操作,是否繁琐,存在无用操作. 配图 ...
- CF721C. Journey
传送门 说实话,这是一道非常简单的DP题,简单到如果放到NOIp第二题可能都有些差强人意,然而我写崩了. 所以简单记录一下. 需要注意的是,这道题的DP应该是从$N$点开始,以1为边界,满足最短路的三 ...
- UVA11987Almost Union-Find(并查集删除节点)
题目链接 题意:n个数(即1-n)和m个操作: 1表示把x和y合并,2表示把x移到y集合里面,3表示统计x集合的元素个数 1,3好说,关键是2操作,可以先把2删除掉,删除的操作可以找一个其他的数字来取 ...
- Yocto开发笔记之《应用程序架构》(QQ交流群:519230208)
QQ群:519230208,为避免广告骚扰,申请时请注明 “开发者” 字样 ======================================================== Eclip ...
- BZOJ4004: [JLOI2015]装备购买
总之就是线性基那一套贪心理论直接做就好了. 然而加强数据后很卡精度的样子. 于是重点在于这个特技:在整数模意义下搞. #include<cstdio> #include<algori ...
- BZOJ2458: [BeiJing2011]最小三角形
类似分治最近点对的方法乱搞一下就行. #include<bits/stdc++.h> #define N 200010 #define M (s+t>>1) using nam ...
- java8 中的时间和数据的变化
java8除了lambda表达式之外还对时间和数组这两块常用API做想应调整, Stream 有几个常用函数: store 排序 (a,b)-> a.compareTo(b) 排出来的结果是正 ...
- apache安装错误error: APR not found解决办法
linux安装时安装种类不同,一些组件包可能不会被安装,导致linux下安装软件的时候缺这个缺那个,今天为大家介绍linux安装apache时报apr找不到的解决办法 方法/步骤 下载依赖包 wg ...
- SQL 字段保留下划线后部分
select SUBSTRING(b.SUMMARY,0,charindex('_',b.SUMMARY))as SUMMARY from UltimusDB.dbo.INCIDENTS b