WPF自定义路由事件(二)
WPF中的路由事件 as U know,和以前Windows消息事件区别不再多讲,这篇博文中,将首先回顾下WPF内置的路由事件的用法,然后在此基础上自定义一个路由事件。
1.WPF内置路由事件
WPF中的大多数事件都是路由事件,WPF有3中路由策略:

具体不多讲,单需要注意的是WPF路由事件是沿着VIsualTree传递的。VisualTree与LogicalTree的区别在于:LogicalTree的叶子节点是构成用户界面的控件(xaml紧密相关),而VisualTree要连控件中的细微结构也算上。VisualTree是LogicalTree的扩展。
reference: Understanding the Visual Tree and Logical Tree in WPF
下面给出一个使用WPF内置路由事件的例子:

<Window x:Class="WPFRoutedEvent.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" >
<Grid x:Name="Grid1" Margin="10" Background="AliceBlue" MouseLeftButtonDown="Grid1_MouseLeftButtonDown">
<StackPanel Background="BurlyWood" Height="200" x:Name="StackPanel1" Button.Click="ButtonInStackPanel_Click" MouseLeftButtonDown="StackPanel1_MouseLeftButtonDown">
<Button x:Name="Button1" Content="RoutedEvent" Click="Button1_Click" />
</StackPanel>
</Grid>
</Window>


using System.Windows;
using System.Windows.Controls;
using System.Windows.Input; namespace WPFRoutedEvent
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
//Grid订阅Button的Click事件
Grid1.AddHandler(Button.ClickEvent, new RoutedEventHandler(ButtonInGrid_Click));
} private void Button1_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Button Clicked.");
//
//e.Handled = true;
} private void ButtonInStackPanel_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("StackPanel Clicked.");
} private void ButtonInGrid_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Grid Clicked.");
} private void Grid1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
MessageBox.Show("Grid Mouse Left button down.");
} private void StackPanel1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
MessageBox.Show("StackPanel Mouse Left button down.");
}
}
}

Button的Click事件是一个路由事件,分别在StackPanel中和Grid中订阅这个事件并进行相应的处理,分别用xaml代码和C#代码如下:
Click="Button1_Click"
Button.Click="ButtonInStackPanel_Click"
Grid1.AddHandler(Button.ClickEvent, new RoutedEventHandler(ButtonInGrid_Click));
StackPanel的MouseLeftButtonDown也是一个路由事件,也可以叫“附加事件”。其实“附加事件”也是路由事件,只是个文字游戏,为什么还要另外起个名字呢?原来路由事件的宿主都是那些拥有可视化实体的界面元素;而附加事件则不具备显示在用户界面上的能力。
常见的附加事件有:
Binding类:SourceUpdated事件、TargetUpdated事件。
Mouse类:MouseEnter事件、MouseLeave事件、MouseDown事件、MouseUp事件等。
Keyboard类:KeyDown事件、KeyUp事件等。
Grid和StackPanel中均如下订阅:
MouseLeftButtonDown="StackPanel1_MouseLeftButtonDown"
程序运行如下:

2.自定义路由事件
前面DebugLZQ写过一篇博文,内容是关于自定义CLR事件的,参考:.NET自定义事件小结。下面来自定义一个WPF路由事件,各位博友可以比较下两者的异同。
创建自定义路由事件大体可以分为三个步骤:
(1)声明并注册路由事件
(2)为路由事件添加CLR事件包装
(3)创建可以激发路由事件的方法
下面我们自定义一个WPF路由事件,我们给事件携带个参数,为此需要创建一个RoutedEventArgs类的派生类。如下:

using System;
using System.Windows; namespace MyRoutedEvent
{
//事件参数
class ReportTimeRoutedEventArgs:RoutedEventArgs
{
public ReportTimeRoutedEventArgs(RoutedEvent routedEvent, object source) : base(routedEvent, source) { } public DateTime ClickTime { get; set; }
}
}

然后,创建一个Button类的派生类并按前面的步骤为其添加路由事件:

using System;
using System.Windows.Controls;
using System.Windows; namespace MyRoutedEvent
{
class TimeButton:Button
{
//声明和注册路由事件\
public static readonly RoutedEvent ReportTimeRoutedEvent =
EventManager.RegisterRoutedEvent("ReportTime", RoutingStrategy.Bubble, typeof(EventHandler<ReportTimeRoutedEventArgs>), typeof(TimeButton));
//CLR事件包装
public event RoutedEventHandler ReportTime
{
add { this.AddHandler(ReportTimeRoutedEvent, value); }
remove { this.RemoveHandler(ReportTimeRoutedEvent, value); }
}
//激发路由事件,借用Click事件的激发方法 protected override void OnClick()
{
base.OnClick();//保证Button原有功能正常使用,Click事件被激发 ReportTimeRoutedEventArgs args = new ReportTimeRoutedEventArgs(ReportTimeRoutedEvent, this);
args.ClickTime = DateTime.Now;
this.RaiseEvent(args);//UIElement及其派生类
} }
}

下面是程序界面的XAML代码,看下如何消费这个路由事件:

<Window x:Class="MyRoutedEvent.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyRoutedEvent"
Title="MainWindow" Height="350" Width="525">
<Grid x:Name="grid1" local:TimeButton.ReportTime="TimeButton_ReportTime"><!---->
<Grid x:Name="grid2">
<Grid x:Name="grid3">
<StackPanel x:Name="stackPanel1">
<ListBox x:Name="listBox1"/>
<local:TimeButton Width="200" Height="200" Background="Aquamarine" ReportTime="TimeButton_ReportTime" /><!---->
</StackPanel>
</Grid>
</Grid>
</Grid>
</Window>

事件处理的后台代码如下:

using System.Windows; namespace MyRoutedEvent
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
} private void TimeButton_ReportTime(object sender, ReportTimeRoutedEventArgs e)//注意参数
{
listBox1.Items.Add(e.ClickTime.ToLongTimeString()+"DebugLZQ");
}
}
}

假如我如果想在后台代码中消费定义的路由事件,该如何做呢?
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent(); this.grid1.AddHandler(TimeButton.OnReportTimeRoutedEvent, new RoutedEventHandler(MainWindow_MEvent));
this.grid1.AddHandler(TimeButton.OnReportTimeRoutedEvent, new RoutedEventHandler(TimeButton_ReportTime1));//监听OnReportTimeRoutedEvent的路由事件 } private void TimeButton_ReportTime(object sender, ReportTimeRoutedEventArgs e)
{
listBox1.Items.Add(e.ClickTime.ToLongTimeString() + "DebugLZQ"); }
private void TimeButton_ReportTime1(object sender, RoutedEventArgs e)
{
ReportTimeRoutedEventArgs ss = e as ReportTimeRoutedEventArgs;//这里使用了一个转换,父类转换程子类的引用,只有转换了,才能被AddHandler调用。 listBox1.Items.Add(ss.ClickTime.ToLongTimeString() + "DebugLZQ"); }
void MainWindow_MEvent(object sender, RoutedEventArgs e)
{
MessageBox.Show("dddd");
}
}
程序运行效果如下:

小结:UIElement类是路由事件和附加事件的分水岭,因为从UIElement类开始才具备了再界面上显示的能力,也因为RaiseEvent、AddHandler和RemoveHandler这些方法也定义在UIElement类中。附加事件也只能算是路由事件的一种用法而不是一个新的概念,其本质还是路由事件。
WPF自定义路由事件(二)的更多相关文章
- WPF:自定义路由事件的实现
路由事件通过EventManager,RegisterRoutedEvent方法注册,通过AddHandler和RemoveHandler来关联和解除关联的事件处理函数:通过RaiseEvent方法来 ...
- 细说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 之路由事件和附加事件(六)
一.消息驱动与直接事件模型 事件的前身是消息(Message).Windows 是消息驱动的系统,运行其上的程序也遵循这个原则.消息的本质就是一条数据,这条消息里面包含着消息的类别,必要的时候还记 ...
随机推荐
- 你不知道的js
1.<a>标签 (1)href属性包含超链接或超链接指向的URL或URL片段,URL 片段是哈希标记(#)前面的名称,哈希标记指定当前文档中的内部目标位置(HTML 元素的 ID).URL ...
- react Immutability 理解
在开发过程中经常会遇到state里有数组和对象的情况,比如当用splice去改变数组再调用setState更新的时候,会发现并没有生效,这是因为react里的state是Immutability(不可 ...
- ESAPI学习笔记
ESAPI是owasp提供的一套API级别的web应用解决方案,本人通过对ESAPI和其提供的demo源码学习发现,关键的不是对其所提供的API的使用,而是其web应用安全防御体系的构建的思 ...
- java:@SuppressWarnings注解
简介:java.lang.SuppressWarnings是J2SE 5.0中标准的Annotation之一.可以标注在类.字段.方法.参数.构造方法,以及局部变量上.作用:告诉编译器忽略指定的警告, ...
- Java知识回顾 (4)Java包装类
一. Java Number 一般地,当需要使用数字的时候,我们通常使用内置数据类型,如:byte.int.long.double 等. 然而,在实际开发过程中,我们经常会遇到需要使用对象,而不是内置 ...
- 【linux】linux下对java程序生成dump文件,并使用IBM Heap Analyzer进行分析,查找定位内存泄漏的问题代码
1.首先,java程序启动在linux,怎么生成dump文件? 1>第一步,首先你需要得到java程序的PID,最简单的方法使用如下命令 ps -ef|grep java 或者如果是docker ...
- ZegGraph属性含义
一.主要内容概念 属性名称 属性值.作用 MasterPane 一个类对象管理多个GraphPane来源于PaneBase.使用MasterPane类都是可选的,GraphPane类可以直接用于一个单 ...
- linux查看及修改文件权限以及相关
linux查看及修改文件权限以及相关 查看文件权限的语句: 在终端输入: ls -l xxx.xxx (xxx.xxx是文件名)那么就会出现相类似的信息,主要都是这些: -rw-rw-r-- 一共有1 ...
- MySQL到底能支持多大的数据量?
MySQL是中小型网站普遍使用的数据库之一,然而,很多人并不清楚MySQL到底能支持多大的数据量,再加上某些国内CMS厂商把数据承载量的责任推给它,导致很多不了解MySQL的站长对它产生了很多误解,那 ...
- DHCP服务原理与搭建(Linux系统+路由器,二选一方案)
大家都知道上网的最基本前提是要在终端上设置IP.子网掩码.网关.DNS等地址信息,在家里或者在办公室很多时候打开电脑后发现就可以上网,并没有手动设置IP.掩码.DNS地址也能上网,这是什么原因呢?其实 ...