如何:处理路由事件

更新:2010 年 7 月

本示例演示冒泡事件的工作方式,以及如何编写可处理路由事件数据的处理程序。

示例

Windows Presentation Foundation (WPF) 中的元素以元素树结构形式排列。 父元素可以参与处理最初由元素树中的子元素引发的事件, 这是由于存在事件路由。

路由事件通常遵循以下两个路由策略之一:冒泡和隧道。 下面的示例将重点放在冒泡事件上,并且使用 ButtonBase.Click 事件来演示路由的工作方式。

下面的示例创建两个 Button 控件并使用 XAML 特性语法向公用的父元素(在本示例中为 StackPanel)附加事件处理程序。 本示例使用特性语法向 StackPanel 父元素附加事件处理程序,而不是为每个 Button 子元素都附加一个事件处理程序。 这个事件处理模式演示了如何使用事件路由技术来减少需要附加处理程序的元素数量。 每个 Button 的所有冒泡事件都通过父元素进行路由。

请注意,在父 StackPanel 元素上,指定为特性的 Click 事件名称可通过命名 Button 类来进行部分限定。 Button 类是一个 ButtonBase 派生类,它在其成员列表中有一个 Click 事件。 如果要处理的事件不在附加有路由事件处理程序的元素的成员列表中,则有必要使用这种部分限定技术来附加事件处理程序。

<StackPanel
  xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
  x:Class="SDKSample.RoutedEventHandle"
  Name="dpanel"
  Button.Click="HandleClick"
>
  <StackPanel.Resources>
      <Style TargetType="{x:Type Button}">
        <Setter Property="Height" Value="20"/>
        <Setter Property="Width" Value="250"/>
        <Setter Property="HorizontalAlignment" Value="Left"/>
      </Style>
  </StackPanel.Resources>
  <Button Name="Button1">Item 1</Button>
  <Button Name="Button2">Item 2</Button>
  <TextBlock Name="results"/>
</StackPanel>
<StackPanel
  xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
  x:Class="SDKSample.RoutedEventHandle"
  Name="dpanel"
  Button.Click="HandleClick"
>
  <StackPanel.Resources>
      <Style TargetType="{x:Type Button}">
        <Setter Property="Height" Value="20"/>
        <Setter Property="Width" Value="250"/>
        <Setter Property="HorizontalAlignment" Value="Left"/>
      </Style>
  </StackPanel.Resources>
  <Button Name="Button1">Item 1</Button>
  <Button Name="Button2">Item 2</Button>
  <TextBlock Name="results"/>
</StackPanel>

下面的示例处理 Click 事件。 该示例报告哪个元素处理该事件以及哪个元素引发该事件。 当用户单击任一按钮时,会执行事件处理程序。

Private eventstr As New Text.StringBuilder()

Private Sub HandleClick(ByVal sender As Object, ByVal args As RoutedEventArgs)
    ' Get the element that handled the event.
    Dim fe As FrameworkElement = DirectCast(sender, FrameworkElement)
    eventstr.Append("Event handled by element named ")
    eventstr.Append(fe.Name)
    eventstr.Append(vbLf)

    ' Get the element that raised the event. 
    Dim fe2 As FrameworkElement = DirectCast(args.Source, FrameworkElement)
    eventstr.Append("Event originated from source element of type ")
    eventstr.Append(args.Source.[GetType]().ToString())
    eventstr.Append(" with Name ")
    eventstr.Append(fe2.Name)
    eventstr.Append(vbLf)

    ' Get the routing strategy.
    eventstr.Append("Event used routing strategy ")
    eventstr.Append(args.RoutedEvent.RoutingStrategy)
    eventstr.Append(vbLf)

    results.Text = eventstr.ToString()
End Sub
public partial class RoutedEventHandle : StackPanel
{
    StringBuilder eventstr = new StringBuilder();
    void HandleClick(object sender, RoutedEventArgs args)
    {
        // Get the element that handled the event.
        FrameworkElement fe = (FrameworkElement)sender;
        eventstr.Append("Event handled by element named ");
        eventstr.Append(fe.Name);
        eventstr.Append("\n");

        // Get the element that raised the event. 
        FrameworkElement fe2 = (FrameworkElement)args.Source;
        eventstr.Append("Event originated from source element of type ");
        eventstr.Append(args.Source.GetType().ToString());
        eventstr.Append(" with Name ");
        eventstr.Append(fe2.Name);
        eventstr.Append("\n");

        // Get the routing strategy.
        eventstr.Append("Event used routing strategy ");
        eventstr.Append(args.RoutedEvent.RoutingStrategy);
        eventstr.Append("\n");

        results.Text = eventstr.ToString();
    }
}

请参见

参考

RoutedEvent

概念

输入概述

路由事件概述

XAML 语法详述

其他资源

事件帮助主题

修订记录

日期

修订记录

原因

2010 年 7 月

添加了事件处理程序示例。

客户反馈