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

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

使用例

次の例に示すように、まず、RegisterRoutedEvent メソッドを使用して RoutedEvent を登録します。 通常、RoutedEvent の静的フィールド名は、サフィックスの Event で終わる必要があります。 この例では、イベントの名前は Tap で、イベントのルーティング方法は Bubble です。 登録呼び出しの後、追加および削除のcommon language runtime (CLR) イベント アクセサーをイベントに提供できます。

ここに示す例では、イベントは OnTap 仮想メソッドによって発生しますが、イベントを発生させる方法やイベントを変更に応答させる方法は必要に応じて決まることに注意してください。

この例では、基本的に Button のサブクラス全体を実装していることに注意してください。また、このサブクラスは、個別のアセンブリとして作成され、個別の Extensible Application Markup Language (XAML) ページでカスタム クラスとしてインスタンス化されています。 これは概念として、サブクラス化されたコントロールは、他のコントロールで構成されるツリーに挿入でき、このような場合、このコントロールのカスタム イベントは、ネイティブな Windows Presentation Foundation (WPF) 要素とまったく同じイベント ルーティング機能を備えるということを示しています。

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

}
<Window  
    xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="https://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>

トンネル イベントも同じように作成されますが、登録呼び出しで RoutingStrategyTunnel に設定される点が異なります。 通常、WPF では、トンネル イベントに "Preview" がプレフィックスとして付加されます。

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

参照

概念

ルーティング イベントの概要

入力の概要

コントロールの作成の概要