WPF事件,路由事件
直接事件模型或CLR事件模型
1事件拥有者
2事件响应者
3事件订阅关系
例如 Window窗口中的控件Button
事件:拥有者Button
事件:Button.Click
事件响应者:Window
事件处理器:Button_click(…..)
事件订阅 Button.Click+=new system.EventHandle(this.Button_Click)
CLR事件模型中事件的拥有者也就是消息的发送者(sender)
private void button_Click(object sender, RoutedEventArgs e)
{
if(sender is Button)
{
MessageBox.Show((sender as Button).Name);
}
}
缺点:事件拥有者与事件的响应者必须建立订阅这条专线,
路由事件
系统路由事件
事件的拥有者只负责激发事件,事件响应者则安装事件侦听器,当有此类型的事件传递至此时,事件响应者就使用事件处理器来响应事件并决定是否事件可以继续传递
路由事件是从叶-----------》根传播的
注册侦听器
C#
this.window.AddHandler(Button.ClickEvent, new
RoutedEventHandler(this.buutonClick));
XAML
<Grid x:Name="gridroot" Margin="10" Button.Click="buutonClick">
Event
private void buutonClick(object sender,
RoutedEventArgs e)
{
MessageBox.Show((e.OriginalSource as Button).Name);
}
自定义路由事件
编写携带参数的事件消息类
1, public class ReportTimeEventArgs:RoutedEventArgs
2, {
3, public
ReportTimeEventArgs(RoutedEvent routedEvent, object source) : base(routedEvent,
source) { }
4, public DateTime ClickTime { get; set; }
5,
}
编写路由事件类
public class TimeButton:Button
{
public static readonly RoutedEvent ReprotTimeEvent=
EventManager.RegisterRoutedEvent("ReprotTime",
RoutingStrategy.Bubble,typeof(EventHandler<ReportTimeEventArgs>),typeof(TimeButton));
// CLR事件包装器作用是把事件路由暴露的像一个直接事件一样类似于属行
public event RoutedEventHandler ReprotTime
{
add{ this.AddHandler(ReprotTimeEvent,value);}
remove{this.RemoveHandler(ReprotTimeEvent,value);}
}
protected override void OnClick()
{
base.OnClick();
ReportTimeEventArgs args = new ReportTimeEventArgs(ReprotTimeEvent,this);//准备事消息
args.ClickTime = DateTime.Now;
this.RaiseEvent(args);
}
}
注册侦听器
<Grid local:TimeButton.ReprotTime="ReprotTimeClick" x:Name="gridroot">
<Grid local:TimeButton.ReprotTime="ReprotTimeClick" x:Name="grid1">
<Grid local:TimeButton.ReprotTime="ReprotTimeClick" x:Name="grid2">
<StackPanel local:TimeButton.ReprotTime="ReprotTimeClick" x:Name="stackPanel">
<ListBox Background="BlueViolet" local:TimeButton.ReprotTime="ReprotTimeClick" x:Name="listBox1">
<local:TimeButton x:Name="timeButton" Width="80" Height="80" Content="报时" local:TimeButton.ReprotTime="ReprotTimeClick"></local:TimeButton>
</ListBox>
</StackPanel>
</Grid>
</Grid>
</Grid>
RoutingStrategy.Bubble,参数指定WPF路由事件的三种策略
1, Bubble(冒泡式):从事件的激发着出发向它上级容器一层一层路由,即有Button 到
window
2
Tunnel(隧道式):与冒泡式相反有Window到Button
3
3Direct(直达式)直接将消息送达事件处理器
WPF事件,路由事件的更多相关文章
- 学习WPF——了解路由事件
入门 我们先来看一个例子 前台代码: 后台代码: 点击按钮的运行效果第一个弹出窗口 第二个弹出窗口: 第三个弹出窗口: 说明 当点击按钮之后,先触发按钮的click事件,再上查找,发现stackpan ...
- 【WPF】路由事件
总结WPF中的路由事件,我将学到的内容分为四部分来逐渐掌握 第一部分:wpf中内置的路由事件 以Button的Click事件来说明内置路由事件的使用 XAML代码: <Window x:Clas ...
- WPF - 善用路由事件
原文:WPF - 善用路由事件 在原来的公司中,编写自定义控件是常常遇到的任务.但这些控件常常拥有一个不怎么好的特点:无论是内部还是外部都没有使用路由事件.那我们应该怎样宰自定义控件开发中使用路由事件 ...
- WPF的路由事件、冒泡事件、隧道事件(预览事件)
本文摘要: 1:什么是路由事件: 2:中断事件路由: 3:自定义路由事件: 4:为什么需要自定义路由事件: 5:什么是冒泡事件和预览事件(隧道事件): 1:什么是路由事件 WPF中的事件为路由事件,所 ...
- WPF:自定义路由事件的实现
路由事件通过EventManager,RegisterRoutedEvent方法注册,通过AddHandler和RemoveHandler来关联和解除关联的事件处理函数:通过RaiseEvent方法来 ...
- 迟到的 WPF 学习 —— 路由事件
1. 理解路由事件:WPF 通过事件路由(event routing)概念增强了传统的事件执行的能力和范围,允许源自某个元素的事件由另一个元素引发,例如,事件路由允许工具栏上的一个按钮点击的事件在被代 ...
- WPF自定义路由事件(二)
WPF中的路由事件 as U know,和以前Windows消息事件区别不再多讲,这篇博文中,将首先回顾下WPF内置的路由事件的用法,然后在此基础上自定义一个路由事件. 1.WPF内置路由事件 WPF ...
- WPF中路由事件的传播
路由事件(RoutedEvent)是WPF中新增的事件,使用起来与传统的事件差别不大, 但传播方式是完全不同的. 路由事件的传播方式 通过RoutingStrategy来定义传播的方式 public ...
- WPF 冒泡路由事件
在WPF中,例如,可以构建一个包含图形的按钮,创建一个具有文本和图片混合内容的标签,或者为了实现滚动或折叠的显示效果在一个特定的容器中放置内容.甚至可以多此重复嵌套,直到达到您所希望的层次深度. 这种 ...
随机推荐
- Examining the Rooms(dp,斯特灵数)
Examining the Rooms Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- [置顶] 有关ListIterator接口的add与remove方法探究
ListIterator接口继承自Iterator接口,新增了add()等方法. 关于ListIterator的add()方法的作用(接口是没有方法实现的,但其实现类对于add()方法的实现机制大致相 ...
- Copy from chromium-dev!
https://app.yinxiang.com/pub/gguangle0/chromium-dev 做了一些搬运工的活..............
- C++关注备注部分知识点
//关注备注部分知识点. #include <iostream> #include <string><span style="white-space:pre&q ...
- T-SQL流程控制
常用的T-SQL流程控制有三种,case ... when ... then...(else)...end (as) ... 判断句式,if判断句式和while循环句式. case...when .. ...
- 【转】 LESS CSS 框架简介
简介 CSS(层叠样式表)是一门历史悠久的标记性语言,同 HTML 一道,被广泛应用于万维网(World Wide Web)中.HTML 主要负责文档结构的定义,CSS 负责文档表现形式或样式的定义. ...
- Caused by: java.lang.UnsupportedClassVersionError: com/zy/example/domain/Student : Unsupported major.minor version 51.0
JVM的版本比jdk的版本老,即JVM的版本低于jdk的版本.换个新版本的就可以解决问题.
- [多线程同步练习]PV操作
看一个较为复杂的生产者-消费者问题: 问题描述 桌子上有一只盘子,每次只能向其中放入一个水果.爸爸专向盘子中放苹果,妈妈专向盘子中放橘子,儿子专等吃盘子中的橘子,女儿专等吃盘子中的苹果.只有盘子为空时 ...
- HTTP性能测试
HTTP性能测试 .note-content {font-family: "Helvetica Neue",Arial,"Hiragino Sans GB",& ...
- mud目录命令说明
目錄結構 以下列出的是 ES2 mudlib 根目錄下標準的子目錄結構,以及簡短的說明. /adm 儲存由系統管理者所管理的程式與資料檔. /std 儲存標準物件的程式碼. /obj 儲存通用物件 ...