首先自定义事件支持事件路由,需要使用 RegisterRoutedEvent 方法注册 RoutedEvent

C#语法
public static RoutedEvent RegisterRoutedEvent(
string name,
RoutingStrategy routingStrategy,
Type handlerType,
Type ownerType
)

参数

name
类型:System.String 路由事件的名称。该名称在所有者类型中必须是唯一的,并且不能为 null 或空字符串。
routingStrategy
类型:System.Windows.RoutingStrategy 作为枚举值的事件的路由策略。
handlerType
类型:System.Type 事件处理程序的类型。该类型必须为委托类型,并且不能为 null。
ownerType
类型:System.Type 路由事件的所有者类类型。该类型不能为 null。
下面我们通过示例展示自定义路由事件:

先看下xaml文件,我们可以看到button中并没有click事件

<Window x:Class="简单自定义路由事件.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>
<Button Content="获取时间" Height="29" HorizontalAlignment="Left" Margin="192,170,0,0" Name="button1" VerticalAlignment="Top" Width="100" />
<TextBox Height="58" HorizontalAlignment="Left" Margin="89,80,0,0" Name="textBox1" VerticalAlignment="Top" Width="339" />
<Label Content="自定义路由事件演示" Height="38" HorizontalAlignment="Left" Margin="141,26,0,0" Name="label1" VerticalAlignment="Top" Width="208" FontSize="22" Background="BlanchedAlmond" />
</Grid>
</Window>

后台代码简单展示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 简单自定义路由事件
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent(); this.button1.AddHandler(Button.ClickEvent,//处理的事件
new RoutedEventHandler(RoutedEvent)); //事件委托 this.MEvent += new RoutedEventHandler(MainWindow_MEvent); } void MainWindow_MEvent(object sender, RoutedEventArgs e)
{
this.textBox1.Text = System.DateTime.Now.ToString();
} private static readonly RoutedEvent MyEvent = EventManager.RegisterRoutedEvent("Event",//路由事件的名称
RoutingStrategy.Direct,//事件的路由策略
typeof(RoutedEvent), //事件处理程序的类型。该类型必须为委托类型
typeof(RoutedEventArgs));//路由事件的所有者类类型 //事件访问器,进行事件提供添加和移除。
public event RoutedEventHandler MEvent
{
add
{
AddHandler(MyEvent, value);
}
remove
{
RemoveHandler(MyEvent, value);
}
}
void RoutedEvent(object o, RoutedEventArgs e)
{
//关联路由事件
RoutedEventArgs args = new RoutedEventArgs(MyEvent);
this.RaiseEvent(args);
} }
}

效果展示:

上面详细的说明建自定义路由事件的基本原理的原理,我们可以通过这样的方式来实现自己的路由事件,这样的代码移致性很高,我们可以需要的时候直接移植.

WPF自定义路由事件(一)的更多相关文章

  1. WPF:自定义路由事件的实现

    路由事件通过EventManager,RegisterRoutedEvent方法注册,通过AddHandler和RemoveHandler来关联和解除关联的事件处理函数:通过RaiseEvent方法来 ...

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

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

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

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

  4. WPF 自定义路由事件

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

  5. Wpf自定义路由事件

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

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

    为student添加附件事件

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

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

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

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

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

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

随机推荐

  1. AngularJS中的$http缓存以及处理多个$http请求

    在AngularJS的实际项目中,经常需要处理多个$http请求,每个$http请求返回一个promise,我们可以把多个promise放到$q.all()方法接受的一个数组实参中去. ■ 处理多个$ ...

  2. 最近无意中看到一个讲解spring mvc的系列,从源码的角度讲解,特记录下来,供以后反复学习

    SpringMVC深度探险(一) —— SpringMVC前传 SpringMVC深度探险(二) —— SpringMVC概览 SpringMVC深度探险(三) —— DispatcherServle ...

  3. Android的Databinding-普通绑定

    1. 使用ActivityBasicBinding binding = DataBindingUtil.setContentView(this, R.layout.main);代替之前使用的setCo ...

  4. jquery操作radio,checkbox

    1. 获取radio选中的value. $('input:radio[name=sex]:checked').val(); 2. 选择 radio 按钮 (Male). $('input:radio[ ...

  5. 权威公布:彻底搞清楚哪些笔记本和台式机主板能够支持42mm SATA M.2 NGFF(2242)接口的固态硬盘!!!

    在京东,天猫上搜寻半天.致电联想客服以及各个固态盘的店小二.都搞不清楚兼容性问题.并且联想客服的回答明显错误,官网描写叙述也错误,客服project师也含糊不清说:要拆机试一试才知道是否兼容. 我就不 ...

  6. WPF如何为程序添加splashScreen(初始屏幕)

    一.考虑到大部分的splashscreen其实都只是一个图片,所以最简单的做法是,先导入一张图片,然后设置它的生成操作为“splash screen” 二.通过程序设置SplashScreen pub ...

  7. C# System.Collections.ArrayList

    using System; using System.Collections; public class SamplesArrayList { public static void Main() { ...

  8. Oracle 12c pdb的数据泵导入导出

    12c推出了可插拔数据库,在一个容器cdb中以多租户的形式同时存在多个数据库pdb.在为pdb做数据泵导入导出时和传统的数据库有少许不同.           1,需要为pdb添加tansnames ...

  9. 【PMP】项目浮动的三种时间

    自由浮动时间 不影响后续工作最早可以开始时间的前提下,这项工作可以拖延的时间叫做自由浮动时间. 总浮动时间 不影响项目总工期的情况下活动可以拖延的总时间. 项目浮动时间 在已经排好的总工期的基础上,领 ...

  10. android开发之打包签名

    android开发之数字签名 http://www.cnblogs.com/fengzhblog/archive/2013/05/06/3063104.html Android系统要求所有的程序经过数 ...