方法: カスタム ルーティング イベントを作成する

カスタム イベントでイベント ルーティングをサポートするには、RegisterRoutedEvent メソッドを使用して RoutedEvent を登録する必要があります。 この例では、カスタム ルーティング イベント作成の基本を紹介します。

次の例に示すように、最初に、RegisterRoutedEvent メソッドを使用して RoutedEvent を登録します。 RoutedEvent 静的フィールドの名前は、Event という接尾辞で終わる決まりになっています。 この例では、イベントの名前は Tap です。イベントのルーティング方法は Bubble です。 登録呼び出し後、イベントの add-and-remove 共通言語ランタイム (CLR) イベント アクセサーを指定できます。

この特別な例では OnTap 仮想メソッド経由でイベントが発生していますが、イベントの発生や変更に対するイベントの反応の仕方はニーズによって変わります。

また、この例では基本的に Buttonのサブクラス全体を実装していることに注意してください。そのサブクラスは別のアセンブリとしてビルドされ、別の XAML ページでカスタム クラスとしてインスタンス化されます。 これは、サブクラス化されたコントロールを他のコントロールで構成されるツリーに挿入でき、この状況では、これらのコントロールのカスタム イベントには、ネイティブ WPF 要素とまったく同じイベント ルーティング機能があるという概念を説明するためです。

public class MyButtonSimple: Button
{
    // Create a custom routed event by first registering a RoutedEventID
    // This event uses the bubbling routing strategy
    public static readonly RoutedEvent TapEvent = EventManager.RegisterRoutedEvent(
        "Tap", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(MyButtonSimple));

    // Provide CLR accessors for the event
    public event RoutedEventHandler Tap
    {
            add { AddHandler(TapEvent, value); }
            remove { RemoveHandler(TapEvent, value); }
    }

    // This method raises the Tap event
    void RaiseTapEvent()
    {
            RoutedEventArgs newEventArgs = new RoutedEventArgs(MyButtonSimple.TapEvent);
            RaiseEvent(newEventArgs);
    }
    // For demonstration purposes we raise the event when the MyButtonSimple is clicked
    protected override void OnClick()
    {
        RaiseTapEvent();
    }
}
Public Class MyButtonSimple
    Inherits Button

    ' Create a custom routed event by first registering a RoutedEventID
    ' This event uses the bubbling routing strategy
    Public Shared ReadOnly TapEvent As RoutedEvent = EventManager.RegisterRoutedEvent("Tap", RoutingStrategy.Bubble, GetType(RoutedEventHandler), GetType(MyButtonSimple))

    ' Provide CLR accessors for the event
    Public Custom Event Tap As RoutedEventHandler
        AddHandler(ByVal value As RoutedEventHandler)
            Me.AddHandler(TapEvent, value)
        End AddHandler

        RemoveHandler(ByVal value As RoutedEventHandler)
            Me.RemoveHandler(TapEvent, value)
        End RemoveHandler

        RaiseEvent(ByVal sender As Object, ByVal e As RoutedEventArgs)
            Me.RaiseEvent(e)
        End RaiseEvent
    End Event

    ' This method raises the Tap event
    Private Sub RaiseTapEvent()
        Dim newEventArgs As New RoutedEventArgs(MyButtonSimple.TapEvent)
        MyBase.RaiseEvent(newEventArgs)
    End Sub

    ' For demonstration purposes we raise the event when the MyButtonSimple is clicked
    Protected Overrides Sub OnClick()
        Me.RaiseTapEvent()
    End Sub

End Class
<Window  
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:custom="clr-namespace:SDKSample;assembly=SDKSampleLibrary"
    x:Class="SDKSample.RoutedEventCustomApp"

    >
    <Window.Resources>
      <Style TargetType="{x:Type custom:MyButtonSimple}">
        <Setter Property="Height" Value="20"/>
        <Setter Property="Width" Value="250"/>
        <Setter Property="HorizontalAlignment" Value="Left"/>
        <Setter Property="Background" Value="#808080"/>
      </Style>
    </Window.Resources>
    <StackPanel Background="LightGray">
        <custom:MyButtonSimple Name="mybtnsimple" Tap="TapHandler">Click to see Tap custom event work</custom:MyButtonSimple>
    </StackPanel>
</Window>

トンネル イベントは同じように作成されますが、RoutingStrategy は登録呼び出しで Tunnel に設定されます。 WPF のトンネル イベントには、接頭辞として "Preview" という単語が付けられる決まりになっています。

バブリング イベントの動作例については、「ルーティング イベントを処理する」を参照してください。

関連項目