Routed Events【pluralsight】
Routing Strategies:
- Direct
- Bubbling
- Tunneling
WHy use them?
- Any UIElement can be a listener
- Common handlers
- Visual Tree Communication
- Event Setter and Event Trigger
Routed Event Example:
<Window x:Class="CustomControlDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
Width="350"
Height="220"
ButtonBase.Click="Window_Click"> //listen to the RoutedEvent:ButtonBase.Click and handle RoutedEvent using Window_Click
<Grid>
<StackPanel Margin="20" ButtonBase.Click="StackPanel_Click"> //listen to the RoutedEvent:ButtonBase.Click and handle RoutedEvent using StackPanel_Click
<Border BorderBrush="Red" BorderThickness="5">
<TextBlock Margin="5"
FontSize="18"
Text="This is a TextBlock" />
</Border>
<Button Margin="10"
Click="Button_Click" //handle the Button.Click event using Button_Click handler
Content="Click me" />
</StackPanel>
</Grid>
</Window>
namespace CustomControlDemo
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
} private void Button_Click(object sender, RoutedEventArgs e)
{
//Button层的event handler,由于ButtonBase.Click是routedEvent所以可以向上bubble
} private void StackPanel_Click(object sender, RoutedEventArgs e)
{
e.Handled = true; //StackPanel层的event handler,不继续向上传
e.Handled = false; //StackPanel层的event handler,继续向上传
} private void Window_Click(object sender, RoutedEventArgs e)
{
//Window层的event handler,已传到最上层
}
}
}
我们也可以不在xaml写handler,直接用后台控制
namespace CustomControlDemo
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
AddHandler(Button.ClickEvent, new RoutedEventHandler(Window_Click), true); //即使e.Handled = true;我们也要routedevent继续传到window层
} private void Button_Click(object sender, RoutedEventArgs e)
{
//Button层的event handler,由于ButtonBase.Click是routedEvent所以可以向上bubble
} private void StackPanel_Click(object sender, RoutedEventArgs e)
{
e.Handled = true;
} private void Window_Click(object sender, RoutedEventArgs e)
{
}
}
}
Custom Routed Events
- register your event
- choose your routing strategy
- provide add and remove CLR wrapper
- Raise your event
Routed Events【pluralsight】的更多相关文章
- Generic【Pluralsight】
prepare Employee Structure namespace CollectIt { public class Employee { public string Name { get; s ...
- 【概率论】2-2:独立事件(Independent Events)
title: [概率论]2-2:独立事件(Independent Events) categories: Mathematic Probability keywords: Independent Ev ...
- 【概率论】1-4:事件的的并集(Union of Events and Statical Swindles)
title: [概率论]1-4:事件的的并集(Union of Events and Statical Swindles) categories: Mathematic Probability key ...
- 【jquery】基础知识
jquery简介 1 jquery是什么 jquery由美国人John Resig创建,至今已吸引了来自世界各地的众多 javascript高手加入其team. jQuery是继prototype之后 ...
- 企业IT管理员IE11升级指南【7】—— Win7和Win8.1上的IE11功能对比
企业IT管理员IE11升级指南 系列: [1]—— Internet Explorer 11增强保护模式 (EPM) 介绍 [2]—— Internet Explorer 11 对Adobe Flas ...
- 【Javascript】重新绑定默认事件
更多内容,请移步 JSCON-简时空 在有一种场景下,你想先屏蔽掉默认的系统事件,而在特定条件下又重新绑定回去. [场景]H5页面,动画欢迎界面,共6帧:想在前5帧中屏蔽掉默认的touchmove事件 ...
- 【故障处理】队列等待之enq IV - contention案例
[故障处理]队列等待之enq IV - contention案例 1.1 BLOG文档结构图 1.2 前言部分 1.2.1 导读和注意事项 各位技术爱好者,看完本文后,你可以掌握如下的技能,也 ...
- 【故障处理】告警日志报“ORA-01565 Unable To open Spfile”
[故障处理]告警日志报"ORA-01565 Unable To open Spfile" 1.1 BLOG文档结构图 1.2 故障分析及解决过程 1.2.1 故障环境介绍 项 ...
- 【ASH】如何导出视图DBA_HIST_ACTIVE_SESS_HISTORY的查询结果数据
[ASH]如何导出视图DBA_HIST_ACTIVE_SESS_HISTORY的查询结果数据 1.1 BLOG文档结构图 1.2 前言部分 1.2.1 导读和注意事项 各位技术爱好者,看完本文后 ...
随机推荐
- 项目中使用的ajax异步读取数据结构设计
设计稍微复杂了一点,完成ajax读取功能涉及到了很多页面.虽然如此,但感觉比较灵活. 和传统方法唯一的区别在于多了一层数据容器控件,里面提供了显示数据的HTML元素及相应的JS方法. 这样数据控件指生 ...
- yeoman的学习
官网地址:http://yeoman.io/ 什么是yeoman? 在上一篇博客已粗劣地提到yeoman的安装和验证.说白了,其实yeoman是生成代码和搭建框架的前端自动化工具.为了做到这些,yeo ...
- [Everyday Mathematics]20150222
设 $$\bex a_0=1,\quad a_1=\frac{1}{2},\quad a_{n+1}=\frac{na_n^2}{1+(n+1)a_n}\ (n\geq 1). \eex$$ 试证: ...
- FastJson只序列化java对象的部分属性
public class Student { private int id; private String name; private int age; //get set方法略 } 如下方法: St ...
- LoadRunner error -27979
4.LoadRunner请求无法找到:在录制Web协议脚本回放脚本的过程中,会出现请求无法找到的现象,而导致脚本运行停止.错误现象:Action.c(41): Error -27979: Reques ...
- 什么是CC攻击,如何防止网站被CC攻击的方法总汇
CC攻击(Challenge Collapsar)是DDOS(分布式拒绝服务)的一种,也是一种常见的网站攻击方法,攻击者通过代理服务器或者肉鸡向向受害主机不停地发大量数据包,造成对方服务器资源耗尽,一 ...
- SoapUI Property
1. Test Suite(Case) Property 选择Test Suite(Case),switch to Custom properties 在request中的引用方式: ${[scope ...
- 正则 提取html标签value
using System.Text.RegularExpressions; //step2: extract expected info //<h1 class="h1user&quo ...
- 安装linux操作系统--浪潮服务器
一直都是在虚拟机上进行安装linux操作系统,在服务器上安装的很少,也没有碰到过没找到驱动的情况,例如什么raid卡驱动,网卡驱动等异常情况的发生. 这次安装了两台服务器,浪潮的提供的服务器,硬盘是两 ...
- Chapter15:派生类
在C++语言中,基类将类型相关的函数与派生类不做改变直接继承的函数区别对待,对于某些函数,基类希望它的派生类各自定义适合自身的版本,此时基类就将这些函数声明为虚函数. 派生类必须将其继承而来的成员函数 ...