Skip to main content
.NET Framework Class Library
RoutedEvent Class

Represents and identifies a routed event and declares its characteristics.

Inheritance Hierarchy
SystemObject
  System.WindowsRoutedEvent

Namespace:   System.Windows
Assembly:  PresentationCore (in PresentationCore.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation
Syntax
<[%$TOPIC/ms589739_en-us_VS_110_2_0_0_0_0%]("System.Windows.Markup.RoutedEventConverter, PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, Custom=null")> _
Public NotInheritable Class RoutedEvent
[[%$TOPIC/ms589739_en-us_VS_110_2_0_1_0_0%]("System.Windows.Markup.RoutedEventConverter, PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, Custom=null")]
public sealed class RoutedEvent
[[%$TOPIC/ms589739_en-us_VS_110_2_0_2_0_0%](L"System.Windows.Markup.RoutedEventConverter, PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, Custom=null")]
public ref class RoutedEvent sealed
[<[%$TOPIC/ms589739_en-us_VS_110_2_0_3_0_0%]>]
[<[%$TOPIC/ms589739_en-us_VS_110_2_0_3_0_1%]("System.Windows.Markup.RoutedEventConverter, PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, Custom=null")>]
type RoutedEvent =  class end
<object property="eventName"/>
- or -
<object property="type.eventName"/>

XAML Values

eventName

An unqualified event name, equivalent to the Name property of the RoutedEvent field, not the actual member name of the RoutedEvent identifier field within a type. Without qualification, eventName must name an event as found in the type that is the TargetType of the current style containing the EventSetter or EventTrigger.

type

The type to use to qualify the event name. If provided without a prefix, type is expected to be a type within the default XAML namespace, and that can be mapped to an existing CLR type. For custom events, or events that are on types outside of the default XAML namespace, the string for type can include a prefix for a mapped XAML namespace. That XAML namespace should contain the XAML type that can map to a backing CLR type, and that CLR type should define the desired routed event identifier. For details on XAML namespace mapping, see XAML Namespaces and Namespace Mapping for WPF XAML.

The RoutedEvent type exposes the following members.

Properties
  NameDescription
Public property HandlerTypeGets the handler type of the routed event.
Public property NameGets the identifying name of the routed event.
Public property OwnerTypeGets the registered owner type of the routed event.
Public property RoutingStrategyGets the routing strategy of the routed event.
Top
Methods
  NameDescription
Public method AddOwnerAssociates another owner type with the routed event represented by a RoutedEvent instance, and enables routing of the event and its handling.
Public method Equals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Public method GetHashCodeServes as the default hash function. (Inherited from Object.)
Public method GetTypeGets the Type of the current instance. (Inherited from Object.)
Public method ToStringReturns the string representation of this RoutedEvent. (Overrides ObjectToString.)
Top
Remarks

This class contains the Name, RoutingStrategy, HandlerType, and OwnerType properties. None of these members can have null values.

This class has a XAML usage that is exclusively intended for providing the value of the RoutedEvent property of an EventTrigger (or derived class), or for the Event property of an EventSetter (or derived class). For more information about EventTrigger, EventSetter, and the XAML usages for those classes, see Routed Events Overview.

Examples

For your custom event to support event routing, you need to register a RoutedEvent using the RegisterRoutedEvent method. This example demonstrates the basics of creating a custom routed event.

As shown in the following example, you first register a RoutedEvent using the RegisterRoutedEvent method. By convention, the RoutedEvent static field name should end with the suffix Event. In this example, the name of the event is Tap and the routing strategy of the event is Bubble. After the registration call, you can provide add-and-remove common language runtime (CLR) event accessors for the event.

Note that even though the event is raised through the OnTap virtual method in this particular example, how you raise your event or how your event responds to changes depends on your needs.

Note also that this example basically implements an entire subclass of Button; that subclass is built as a separate assembly and then instantiated as a custom class on a separate Extensible Application Markup Language (XAML) page. This is to illustrate the concept that subclassed controls can be inserted into trees composed of other controls, and that in this situation, custom events on these controls have the very same event routing capabilities as any native Windows Presentation Foundation (WPF) element does.

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="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>

Tunneling events are created the same way, but with RoutingStrategy set to Tunnel in the registration call. By convention, tunneling events in WPF are prefixed with the word "Preview".

To see an example of how bubbling events work, see How to: Handle a Routed Event.

Version Information

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Platforms

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.