方法 : ルーティング イベントを処理する

更新 : 2010 年 7 月

バブル イベントの動作と、ルーティング イベント データを処理できるハンドラーを作成する方法を次の例に示します。

使用例

Windows Presentation Foundation (WPF) では、要素は、要素ツリー構造体に配置されます。 親要素は、要素ツリー内で最初に子要素が発生させたイベントの処理に参加できます。 これは、イベント ルーティングにより可能です。

ルーティング イベントは通常、バブルまたはトンネルの 2 つのルーティング方法のいずれかに従います。 この例では、バブル イベントに焦点を当て、ButtonBase.Click イベントを使用してルーティングの動作を示します。

次の例では、2 つの Button コントロールを作成し、XAML 属性構文を使用してイベント ハンドラーを共通の親要素 (この例では StackPanel) にアタッチします。 この例では、各 Button 子要素の個々のイベント ハンドラーをアタッチするのではなく、属性構文を使用してイベント ハンドラーを StackPanel 親要素にアタッチします。 このイベント処理パターンは、ハンドラーがアタッチされる要素の数を減らすための手法としてイベント ルーティングを使用する方法を示します。 各 Button のすべてのバブル イベントが親要素を通じてルーティングされます。

StackPanel 要素では、属性として指定された Click イベント名は、Button クラスに名前を付けて部分的に修飾されます。 Button クラスは、そのメンバーの一覧に Click イベントを持つ ButtonBase 派生クラスです。 イベント ハンドラーをアタッチするためのこの部分修飾手法が必要になるのは、ルーティング イベント ハンドラーがアタッチされる要素のメンバー一覧に、処理されているイベントが存在しない場合です。

<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 月

イベント ハンドラーの例を追加しました。

カスタマー フィードバック