次の方法で共有


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

この例では、バブル イベントのしくみと、ルーティング イベント データを処理できるハンドラーを記述する方法を示します。

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

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

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

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

<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 イベントを処理します。 この例では、イベントを処理する要素とイベントを発生させる要素を報告します。 イベント ハンドラーは、ユーザーがいずれかのボタンをクリックしたときに実行されます。

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

関連項目