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】的更多相关文章

  1. Generic【Pluralsight】

    prepare Employee Structure namespace CollectIt { public class Employee { public string Name { get; s ...

  2. 【概率论】2-2:独立事件(Independent Events)

    title: [概率论]2-2:独立事件(Independent Events) categories: Mathematic Probability keywords: Independent Ev ...

  3. 【概率论】1-4:事件的的并集(Union of Events and Statical Swindles)

    title: [概率论]1-4:事件的的并集(Union of Events and Statical Swindles) categories: Mathematic Probability key ...

  4. 【jquery】基础知识

    jquery简介 1 jquery是什么 jquery由美国人John Resig创建,至今已吸引了来自世界各地的众多 javascript高手加入其team. jQuery是继prototype之后 ...

  5. 企业IT管理员IE11升级指南【7】—— Win7和Win8.1上的IE11功能对比

    企业IT管理员IE11升级指南 系列: [1]—— Internet Explorer 11增强保护模式 (EPM) 介绍 [2]—— Internet Explorer 11 对Adobe Flas ...

  6. 【Javascript】重新绑定默认事件

    更多内容,请移步 JSCON-简时空 在有一种场景下,你想先屏蔽掉默认的系统事件,而在特定条件下又重新绑定回去. [场景]H5页面,动画欢迎界面,共6帧:想在前5帧中屏蔽掉默认的touchmove事件 ...

  7. 【故障处理】队列等待之enq IV - contention案例

    [故障处理]队列等待之enq IV -  contention案例 1.1  BLOG文档结构图 1.2  前言部分 1.2.1  导读和注意事项 各位技术爱好者,看完本文后,你可以掌握如下的技能,也 ...

  8. 【故障处理】告警日志报“ORA-01565 Unable To open Spfile”

    [故障处理]告警日志报"ORA-01565 Unable To open Spfile" 1.1  BLOG文档结构图 1.2  故障分析及解决过程 1.2.1  故障环境介绍 项 ...

  9. 【ASH】如何导出视图DBA_HIST_ACTIVE_SESS_HISTORY的查询结果数据

    [ASH]如何导出视图DBA_HIST_ACTIVE_SESS_HISTORY的查询结果数据 1.1  BLOG文档结构图 1.2  前言部分 1.2.1  导读和注意事项 各位技术爱好者,看完本文后 ...

随机推荐

  1. django - raw sql - 注意点!

    现在的自己已经不怕mysql,已经熟悉了sql “搜索优化专家有时候都无法顾全所有,更何况ORM” 两种方法执行 sql原生语句 tablename.objects.raw() - 这样的语句,只能执 ...

  2. Spark源码阅读(1): Stage划分

    Spark中job由action动作生成,那么stage是如何划分的呢?一般的解答是根据宽窄依赖划分.那么我们深入源码看看吧 一个action 例如count,会在多次runJob中传递,最终会到一个 ...

  3. 本地工程提交github

    1. 首先在github上创建一个新的Repository 2. 在本地windows机器上装上git 3. 建立一个文件夹,以后就用这个文件夹作为与Repository对应的库文件夹 4. 输入一下 ...

  4. SqlDataAdapter的update方法

    公司项目需要,需要将旧数据升级.所谓的旧数据指密码,密码经过了加密处理,但是可逆的.现将加密算法进行了更新,因此需要同步处理系统中已有的旧数据. 所有的数据存储在一个表中,简单的说是数据批量更新.自动 ...

  5. Android下EditText的hint的一种显示效果------FloatLabelLayout

    效果: 此为EditText的一种细节,平时可能用的不多,但是用户体验蛮好的,特别是当注册页面的项目很多的时候,加上这种效果,体验更好 仅以此记录,仅供学习参考. 参考地址:https://gist. ...

  6. Android 一步一步教你使用ViewDragHelper

    在自定义viewgroup的时候 要重写onInterceptTouchEvent和onTouchEvent 这2个方法 是非常麻烦的事情,好在谷歌后来 推出了ViewDragHelper这个类.可以 ...

  7. Amarino例程无法使用的问题

    Serial.begin(9600); 而不是用它的57600

  8. Java并发编程-总纲

    Java 原生支持并发,基本的底层同步包括:synchronized,用来标示一个方法(普通,静态)或者一个块需要同步执行(某一时刻,只允许一个线程在执行代码块).volatile,用来标识一个变量是 ...

  9. php时间函数

    PHP中的时间函数有这么些:(1)date用法: date(格式,[时间]);如果没有时间参数,则使用当前时间. 格式是一个字符串,其中以下字符有特殊意义:U 替换成从一个起始时间(好象是1970年1 ...

  10. echart图表控件配置入门(一)

    现在主流的web图表控件主要有hightchart.fusionchart.echart: echart作为百度前端部门近期推出的一个基于html5的免费图表控件,以其丰富图表类型和良好的兼容性速度得 ...