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. xunsearch迅搜体验

    安装与启动 http://www.xunsearch.com/doc/php/guide/start.installation 编写配置文件 http://www.xunsearch.com/doc/ ...

  2. Java程序执行过程

    首先,写好Java代码,保存到硬盘中.然后在命令行中输入: javac ClassName.java 此时,这个Java类文件将编译成字节码(.class)文件.如果用Eclipse等IDE开发工具, ...

  3. SpatiaLite 各版本数据库差异

    SpatiaLite 生成的数据库,3.0版本与4.0版本的表geometry_columns结构发生变化. 这是3.0版本的结构: 这是4.0版本的结构: 主要差别是type和coord_dimen ...

  4. anything vs everything

    everything多用于肯定而anything多用于否定和疑问语气 anything 1) 任何事情/东西,可以用在肯定句/否定句/疑问句中.如:You can take anything you ...

  5. Masonry 固定宽度 等间距

    -(void)makeEqualDisViews:(NSArray *)views inView:(UIView *)containerView LRpadding:(CGFloat)LRpaddin ...

  6. LoadRunner参数数组

    参数数组提供了对一类参数集中存放的机制,其中LR内置的几个函数有:lr_paramarr_idx().lr_paramarr_len().lr_paramarr_random() 同时参数数组必须满足 ...

  7. ASP.NET MVC Razor视图引擎攻略

    --引子 看下面一段MVC 2.0的代码. <%if (Model != null){%> <p><%=Model%></p><%}%>&l ...

  8. C#语言基础01

    Console.WriteLine("hello"); Console.ReadKey();// 按一个按键继续执行 string s=Console.ReadLine();//用 ...

  9. sendip简单使用

    sendip是linux下一个比较好用的发包软件,简单记录一下它的用法 下载源码,编译安装后,可通过  man sendip,查看具体选项介绍,其中说明sendip支持的协议包括:ipv4 ipv6 ...

  10. C++学习之路--类的构建以及数据转换存储

    注意理解下面的代码,数据的处理与转换. 头文件: #ifndef STACK_H #define STACK_H class Stack { struct Link { void* data; Lin ...