* Reset branch for WPF changes * Convert BMP to PNG; fix link-out-of-scope err * Add snippets for WPF... 6794 files!!!! * Add missing snippets * update file updated between migration * Fix paths to include * update breadcrumb and toc * fix index links * fix index links * fix index links * fix markdown
2.9 KiB
title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
| title | ms.date | dev_langs | helpviewer_keywords | ms.assetid | ||||
|---|---|---|---|---|---|---|---|---|
| How to: Create a Custom Routed Event | 03/30/2017 |
|
|
b79f459a-1c3f-4045-b2d4-1659cc8eaa3c |
How to: Create a Custom Routed Event
For your custom event to support event routing, you need to register a xref:System.Windows.RoutedEvent using the xref:System.Windows.EventManager.RegisterRoutedEvent%2A method. This example demonstrates the basics of creating a custom routed event.
Example
As shown in the following example, you first register a xref:System.Windows.RoutedEvent using the xref:System.Windows.EventManager.RegisterRoutedEvent%2A method. By convention, the xref:System.Windows.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 xref:System.Windows.RoutingStrategy.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 xref:System.Windows.Controls.Button; that subclass is built as a separate assembly and then instantiated as a custom class on a separate [!INCLUDETLA#tla_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 [!INCLUDETLA#tla_winclient] element does.
[!code-csharpRoutedEventCustom#CustomClass] [!code-vbRoutedEventCustom#CustomClass]
[!code-xamlRoutedEventCustom#Page]
Tunneling events are created the same way, but with xref:System.Windows.RoutedEvent.RoutingStrategy%2A set to xref:System.Windows.RoutingStrategy.Tunnel in the registration call. By convention, tunneling events in [!INCLUDETLA2#tla_winclient] are prefixed with the word "Preview".
To see an example of how bubbling events work, see Handle a Routed Event.