共用方式為


HOW TO:處理路由事件

更新:2010 年 7 月

本範例顯示反昇事件的運作方式,以及如何撰寫處理常式來處理路由事件資料。

範例

在 Windows Presentation Foundation (WPF) 中,項目會排列成項目樹狀結構。 父項目可參與處理一開始由項目樹狀結構中的子項目引發的事件。 可以這麼做是因為事件路由的緣故。

路由事件通常會遵循兩種路由策略的其中一種,即反昇或通道。 這個範例將著重在反昇事件,並使用 ButtonBase.Click 事件顯示路由的運作方式。

下列範例會建立兩個 Button 控制項,並且使用 XAML 屬性語法將事件處理常式附加到通用父項目,也就是這個範例中的 StackPanel。 此範例不會針對每個 Button 子項目附加個別事件處理常式,而是使用屬性語法附加事件處理常式到 StackPanel 父項目。 這種事件處理模式顯示如何使用事件路由此一方式,來減少附加處理常式的目標項目數。 每個 Button 的所有反昇事件都會透過父項目路由。

請注意,在父 StackPanel 項目上,指定為屬性的 Click 事件名稱是藉由命名 Button 類別來部分限定的。 Button 類別是 ButtonBase 衍生類別 (Derived Class),在其成員清單中有 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 語法詳細資料

其他資源

事件 HOW TO 主題

變更記錄

日期

記錄

原因

2010 年 7 月

新增事件處理常式的範例。

客戶回函。