(一)什么时路由事件
功能定义:路由事件是一种可以针对元素树中的多个侦听器(而不是仅针对引发该事件的对象)调用处理程序的事件。
实现定义:路由事件是一个 CLR 事件,可以由 RoutedEvent 类的实例提供支持并由 Windows Presentation Foundation (WPF) 事件系统来处理
(二)路由事件的划分
(1)冒泡:针对事件源调用事件处理程序。路由事件随后会路由到后续的父元素,直到到达元素树的根。
(2)隧道:最初将在元素树的根处调用事件处理程序。随后,路由事件将朝着路由事件的源节点元素(即引发路由事件的元素)方向,沿路由线路传播到后续的子元素。
(3)直接:只有源元素本身才有机会调用处理程序以进行响应。这与 Windows Forms用于事件的“路由”相似。但是,与标准 CLR 事件不同的是,直接路由事件支持类处理而且可以由 EventSetter 和 EventTrigger 使用。
(三)e.Handled=true“已处理”
如果将 Handled 设置为 true,以此将事件标记为“已处理”,则将“停止”隧道路由或冒泡路由,同时,类处理程序在某个路由点处理的所有事件的路由也将“停止”。
(四)下面列子事件执行循序(操作都是点击TextBlock)
(1)当为冒泡事件时:TextBlock-->Grid-->Window
(2)当为隧道事件时:Window-->Grid-->TextBlock
关于e.Handled=true在列子中的应用理解:e.Handled==true放在哪个事件中执行后,哪个事件之后的路由事件不在执行。

 

(五)代码

(1)UI代码

<Window x:Class="WpfEvent.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" x:Name="window" >
    <Grid Width="300" Height="200" x:Name="grid"  Background="Red">     
        <TextBlock x:Name="tbx" Width="100" Height="27" Background="Blue" />
    </Grid>
</Window>

(2)后台代码

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 WpfEvent
{
    //(一)什么时路由事件
    //功能定义:路由事件是一种可以针对元素树中的多个侦听器(而不是仅针对引发该事件的对象)调用处理程序的事件。
    //实现定义:路由事件是一个 CLR 事件,可以由 RoutedEvent 类的实例提供支持并由 Windows Presentation Foundation (WPF) 事件系统来处理
    //(二)路由事件的划分
    //(1)冒泡:针对事件源调用事件处理程序。路由事件随后会路由到后续的父元素,直到到达元素树的根。
    //(2)隧道:最初将在元素树的根处调用事件处理程序。随后,路由事件将朝着路由事件的源节点元素(即引发路由事件的元素)方向,沿路由线路传播到后续的子元素。
    //(3)直接:只有源元素本身才有机会调用处理程序以进行响应。这与 Windows Forms用于事件的“路由”相似。但是,与标准 CLR 事件不同的是,直接路由事件支持类处理而且可以由 EventSetter 和 EventTrigger 使用。
    //(三)e.Handled=true“已处理”
    //如果将 Handled 设置为 true,以此将事件标记为“已处理”,则将“停止”隧道路由或冒泡路由,同时,类处理程序在某个路由点处理的所有事件的路由也将“停止”。
    //(四)下面列子事件执行循序(操作都是点击TextBlock)
    //(1)当为冒泡事件时:TextBlock-->Grid-->Window
    //(2)当为隧道事件时:Window-->Grid-->TextBlock
    //关于e.Handled=true在列子中的应用理解:e.Handled==true放在哪个事件中执行后,哪个事件之后的路由事件不在执行。   
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.grid.MouseLeftButtonDown += new MouseButtonEventHandler(grid_MouseLeftButtonDown);
            this.tbx.MouseLeftButtonDown += new MouseButtonEventHandler(tbx_MouseLeftButtonDown);
            this.MouseLeftButtonDown += new MouseButtonEventHandler(window_MouseLeftButtonDown);

            //this.grid.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(grid_PreviewMouseLeftButtonDown);
            //this.tbx.PreviewMouseLeftButtonDown+=new MouseButtonEventHandler(tbx_PreviewMouseLeftButtonDown);
            //this.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(window_PreviewMouseLeftButtonDown);

        }

      
     
        private void grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            MessageBox.Show("我是Grid");           
        }

        private void window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            MessageBox.Show("我是窗体");
           
        }

        private void tbx_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            MessageBox.Show("我是TextBlock");
            e.Handled = true;
           
        }



        private void window_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            MessageBox.Show("我是窗体");
           

        }

        private void grid_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            MessageBox.Show("我是Grid");
            //e.Handled = true;
          
        }

        private void tbx_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            MessageBox.Show("我是TextBlock");
           
        }
      
    }
}