路由事件通过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:自定义路由事件的实现的更多相关文章

  1. WPF自定义路由事件(二)

    WPF中的路由事件 as U know,和以前Windows消息事件区别不再多讲,这篇博文中,将首先回顾下WPF内置的路由事件的用法,然后在此基础上自定义一个路由事件. 1.WPF内置路由事件 WPF ...

  2. 细说WPF自定义路由事件

    WPF中的路由事件 as U know,和以前Windows消息事件区别不再多讲,这篇博文中,将首先回顾下WPF内置的路由事件的用法,然后在此基础上自定义一个路由事件. 1.WPF内置路由事件   W ...

  3. WPF 自定义路由事件

    如何:创建自定义路由事件 首先自定义事件支持事件路由,需要使用 RegisterRoutedEvent 方法注册 RoutedEvent C#语法 public static RoutedEvent ...

  4. Wpf自定义路由事件

    创建自定义路由事件大体可以分为三个步骤: ①声明并注册路由事件. ②为路由事件添加CLR事件包装. ③创建可以激发路由事件的方法. 以ButtonBase类中代码为例展示这3个步骤: public a ...

  5. WPF自定义路由事件(一)

    首先自定义事件支持事件路由,需要使用 RegisterRoutedEvent 方法注册 RoutedEvent C#语法 public static RoutedEvent RegisterRoute ...

  6. WPF 自定义路由事件 与 附加路由事件

    为student添加附件事件

  7. WPF自学入门(四)WPF路由事件之自定义路由事件

    在上一遍博文中写到了内置路由事件,其实除了内置的路由事件,我们也可以进行自定义路由事件.接下来我们一起来看一下WPF中的自定义路由事件怎么进行创建吧. 创建自定义路由事件分为3个步骤: 1.声明并注册 ...

  8. WPF路由事件三:自定义路由事件

    与依赖项属性类似,WPF也为路由事件提供了WPF事件系统这一组成.为一个类型添加一个路由事件的方式与为类型添加依赖项属性的方法类似,添加一个自定义路由事件的步骤: 一.声明路由事件变量并注册:定义只读 ...

  9. WPF的路由事件、冒泡事件、隧道事件(预览事件)

    本文摘要: 1:什么是路由事件: 2:中断事件路由: 3:自定义路由事件: 4:为什么需要自定义路由事件: 5:什么是冒泡事件和预览事件(隧道事件): 1:什么是路由事件 WPF中的事件为路由事件,所 ...

随机推荐

  1. Character literal must contain exactly one character -- 一天一点小知识

    编程语言高度抽象化以后,错误也越来越让人难以理解了, NET编程最常见的一个错误, Object not set to the reference ,过了好久,才明白过来, 就是不明白为啥微软不说   ...

  2. [TYVJ]1519 博彩

    传送门 AC自动机模板题,好吧我只是单纯的搞个AC自动机的模板. //TYVJ 1519 //by Cydiater //2016.10.18 #include <iostream> #i ...

  3. BZOJ3813: 奇数国

    传送门 欧拉函数+线段树 因为只有60个素数,所以把状态压成long long的形式.用线段树维护区间和和区间和中有多少个质数.然后xjb搞搞就行了,具体参见代码. //BZOJ 3813 //by ...

  4. 今天执行grep命令差点把服务器搞崩

    grep "rst" -r ./ >> a.log 今天执行这个命令差点把服务器搞崩了. 本意是查找所有源代码文件中含有rst字符串的行,打印到文件a.log中,然后进 ...

  5. linux常用快捷键

    linux常用快捷键:ctrl+c 强制终止当前命令ctrl+l 清屏ctrl+a 光标移动到命令行首ctrl+e 光标移动到命令行尾ctrl+u 从光标合所在的位置删除到行首ctrl+z 把命令放到 ...

  6. 11 Clever Methods of Overfitting and how to avoid them

    11 Clever Methods of Overfitting and how to avoid them Overfitting is the bane of Data Science in th ...

  7. 《深入理解bootstrap》读书笔记:第三章 CSS布局

    一. 概述一下理念 bootstrap基于H5开发.提倡移动先行(媒询声明是必须的),对浏览器支持面不是很广. 响应式图片:max-width:100% height:auto; 可以加上:.img- ...

  8. 菜单each+hover

    <script type="text/javascript"> $(document).ready(function(){ var src1= new Array('i ...

  9. ELMAH日志组件数据库脚本

    CREATE TABLE dbo.ELMAH_Error ( ErrorId UNIQUEIDENTIFIER NOT NULL, Application NVARCHAR() COLLATE SQL ...

  10. Java 自动装箱与拆箱

    Java 自动装箱与拆箱(Autoboxing and unboxing)   什么是自动装箱拆箱 基本数据类型的自动装箱(autoboxing).拆箱(unboxing)是自J2SE 5.0开始提供 ...