HOW TO:處理路由事件How to: Handle a Routed Event

此範例示範事件反昇事件運作方式,以及如何撰寫可處理路由事件資料的處理常式。This example shows how bubbling events work and how to write a handler that can process the routed event data.

範例Example

Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) 中,項目會以項目樹狀結構排列。In Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF), elements are arranged in an element tree structure. 父項目可以參與處理一開始是由項目樹狀結構中子項目所引發的事件。The parent element can participate in the handling of events that are initially raised by child elements in the element tree. 這可能是事件路由所造成。This is possible because of event routing.

路由事件通常會遵循兩個路由策略中的其中一個:事件反昇或通道。Routed events typically follow one of two routing strategies, bubbling or tunneling. 此範例著重於事件反昇事件,並使用ButtonBase.Click事件,以顯示路由運作方式。This example focuses on the bubbling event and uses the ButtonBase.Click event to show how routing works.

下列範例會建立兩個Button控制項,並使用XAMLXAML屬性語法,以將事件處理常式附加至通用的父項目,這在此範例中是StackPanelThe following example creates two Button controls and uses XAMLXAML attribute syntax to attach an event handler to a common parent element, which in this example is StackPanel. 而不是每個附加個別的事件處理常式Button子元素,此範例使用屬性語法來附加事件處理常式StackPanel父項目。Instead of attaching individual event handlers for each Button child element, the example uses attribute syntax to attach the event handler to the StackPanel parent element. 此事件處理模式示範如何使用事件路由,作為減少已附加處理常式之項目數的技術。This event-handling pattern shows how to use event routing as a technique for reducing the number of elements where a handler is attached. 每個的所有事件反昇事件Button路由到父項目。All the bubbling events for each Button route through the parent element.

請注意,其父系StackPanel項目,Click指定為部分限定屬性所命名的事件名稱Button類別。Note that on the parent StackPanel element, the Click event name specified as the attribute is partially qualified by naming the Button class. Button類別是ButtonBase衍生的類別具有Click的成員清單中的事件。The Button class is a ButtonBase derived class that has the Click event in its members listing. 如果所處理的事件不存在於附加路由事件處理常式之項目的成員清單中,則需要有附加事件處理常式的這個部分限定方法。This partial qualification technique for attaching an event handler is necessary if the event that is being handled does not exist in the members listing of the element where the routed event handler is attached.

<StackPanel
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://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事件。The following example handles the Click event. 此範例會報告哪些項目處理事件以及哪個項目引發事件。The example reports which element handles the event and which element raises the event. 使用者按一下任一按鈕時,就會執行事件處理常式。The event handler is executed when the user clicks either button.

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();
    }
}
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

另請參閱See also