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中的事件为路由事件,所 ...
随机推荐
- POJ3207Ikki's Story IV - Panda's Trick(模板题)
题目链接 题意:平面上,一个圆,圆的边上按顺时针放着n个点.现在要连m条边,比如a,b,那么a到b可以从圆的内部连接,也可以从圆的外部连接.给你的信息中,每个点最多只会连接的一条边.问能不能连接这m条 ...
- HD1160FatMouse's Speed(最长单调递增子序列)
FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- centos搭建https协议的tomcat和apache服务器以及nginx服务器,mysql php
Apache HTTP Server(简称 Apache)是 Apache 软件基金会的一个开放源码的网页服务器,可以在大多数计算机操作系统中运行,由于其多平台和安全性被广泛使用,是最流行的 Web ...
- JS-事件之鼠标、键盘都能控制的下拉选框效果
<script type="text/javascript"> window.onload=function(e){ var box=document.getEleme ...
- Ubuntu 12.04 root账户开启及密码重设
以普通用户登录,root账号的开启.关闭和密码设置,命令如下: sudo passwd -u root # 启用root账户 sudo passwd root # 设置root 密码(包括重设) su ...
- php 简单分页类
/** file: page.class.php 完美分页类 Page */ class Page { private $total; //数据表中总记录数 privat ...
- centos设置编码
CentOS6.5解决中文乱码与设置字符集 Windows的默认编码为GBK,Linux的默认编码为UTF-8.在Windows下编辑的中文,在Linux下显示为乱码.为了解决此问题,修改Linux的 ...
- 转移大于2m的pdf文件到另外一个文件夹
转移大于2m的pdf文件到另外一个文件夹 remove_moret2M_pdfs.py # -*- coding: utf-8 -*- """ Created on Mo ...
- Spring解析实践
这几天重新把传智播客的黎活明的Spring2.5的教程学习了一遍,跟着上面的解析Spring的过程跟着制作了一个简单的Spring IOC和Spring AOP,先在贴上来给大家参考一下. 1:管理B ...
- 20145212 实验四《Andoid开发基础》
20145212 实验四<Andoid开发基础> 实验内容 安装Android Studio 运行安卓AVD模拟器 使用Android运行出模拟手机并显示自己的学号 实验过程 一.安装An ...