路由事件通过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. PHPstorm激活

    最近想学习一下PHP 于是下载了很不错的phpstorm  但这老外的工具是要购买正版的 所以就搜了一下破解激活的教程 发现现在网上的在线破解在2016.2版本里面大多已被封杀 尝试了本地破解也发现大 ...

  2. HDU 1394 Minimum Inversion Number(最小逆序数/暴力 线段树 树状数组 归并排序)

    题目链接: 传送门 Minimum Inversion Number Time Limit: 1000MS     Memory Limit: 32768 K Description The inve ...

  3. DropZone

    JavaScript 文件拖拽上传插件 dropzone.js 介绍 February 19, 2014 / 编程指南 dropzone.js 是一个开源的 JavaScript 库,提供 AJAX ...

  4. POJ2635The Embarrassed Cryptographer(大数取余+素数筛选+好题)

    题目链接 题意:K是由两个素数乘积,如果最小的素数小于L,输出BAD最小的素数,否则输出GOOD 分析 素数打表将 L 大点的素数打出来,一定要比L大,然后就开始枚举,只需K对 素数 取余 看看是否为 ...

  5. segmentfault.com mongo出识以及对数组的操作

    https://segmentfault.com/a/1190000003951602 首先推荐个工具,no-sql-manager-for-mongodb-professional,虽然收费,但是每 ...

  6. Sublime Text 3 快捷键整理

    选择类Ctrl+D 选中光标所占的文本,继续操作则会选中下一个相同的文本.Alt+F3 选中文本按下快捷键,即可一次性选择全部的相同文本进行同时编辑.Ctrl+L 选中整行,继续操作则继续选择下一行, ...

  7. matlab解三元二次方程组

    C1=7.0863; C2=6.8971; C3=0.4929; C4=0.8131; C5=1.8240; C6=3.8108; C7=3.7318; C8=-2.2238; C9=1.9905; ...

  8. git config --global core.autocrlf false

    git config --global core.autocrlf  false warning: LF will be replaced by CRLF in .idea/vcs.xml.The f ...

  9. conv2、filter2、imfilter的区别

    conv2.filter2.imfilter的区别 -------------------------------------conv2函数------------------------------ ...

  10. macbook pro的usb串口失效的的处理方法

    macbook pro的usb串口失效的的处理方法 2011-08-24 12:14:32|  分类: mac|举报|字号 订阅     今天开电脑,无端端一个usb的串口失效了,接入鼠标 iphon ...