Routed Event Code Snippet

Posted Wednesday, October 15, 2008 1:51 PM by Nathan Zaugg

framework35 I find it really strange that there are code snippets for Dependency Properties but not Routed Events!  Routed Events allow events to tunnel (preview) events up a visual tree and bubble them back down the visual tree.  Any control along the tree can subscribe to these events and handle them.  You can also choose to see events even if they have been handled. 

This makes RoutedEvents very nice for use in XAML!  For example, lets say you have a StackPanel and you want to know about any button clicks happening in that panel, you could simply use the following XAML:

<StackPanel ButtonBase.Click="MyButtonEventHandler">
    <Button Height="50" Content="Button 1" />
    <Button Height="50" Content="Button 2" />
    <Button Height="50" Content="Button 3" />
    <Button Height="50" Content="Button 4" />

Now when any one of the buttons are clicked, the stack panel will receive notification.  Just like Attached Properties, the stack panel does not need to have a Click event.  If a button does not exist inside of the stack panel there is no problem, you just won't receive any notification.  The buttons do not even have to be direct children of the stack panel for this to work so if we wanted we could put them inside of another stack panel or any other content control.

I really think it strange that there was no snippet for Routed Events (being as handy as they are) so I took the time to write one.

Lets make a code snippet for a Routed Event, here is how:

Create a Custom Snippet

  1. Find a snippet you plan to modify in the Manage Snippets Dialog (Tools -> Code Snippets Manager)
  2. Open the Snippet using File -> Open File and open the file:
    C:\Program Files\Microsoft Visual Studio 9.0\VC#\Snippets\1033\NetFX30\propdp.snippet
  3. Edit the declarations (like):
              <ToolTip>Event Name</ToolTip>
  4. Edit the snippet using $name$ for any literal replacement.
  5. Be sure to change the shortcut in the XML to "revent".
  6. Save the file as "revent.snippet"
  7. Your visual studio instance should now see the snippet. 


Here is the snippet code I used:

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="">
    <CodeSnippet Format="1.0.0">
            <Title>Define a RoutedEvent</Title>
            <Description>Code snippet for a event using RoutedEvent</Description>
            <Author>Nathan Zaugg</Author>
                    <ToolTip>Event Handler Type Type</ToolTip>
                    <ToolTip>Event Name</ToolTip>
                    <ToolTip>The owning class of this Property.  
Typically the class that it is declared in.</ToolTip> <Default>ownerclass</Default> </Literal> <Literal> <ID>routingstratigy</ID> <ToolTip>The routing stratigy for this event.</ToolTip> <Default>Bubble</Default> </Literal> </Declarations> <Code Language="csharp"> <![CDATA[ // Provide CLR accessors for the event public event RoutedEventHandler $name$ { add { AddHandler($name$Event, value); } remove { RemoveHandler($name$Event, value); } } // Using a RoutedEvent public static readonly RoutedEvent $name$Event = EventManager.RegisterRoutedEvent( "$name$", RoutingStrategy.$routingstratigy$, typeof($eventhandlertype$), typeof($ownerclass$)); $end$]]> </Code> </Snippet> </CodeSnippet> </CodeSnippets>

When you are done it should look like this:

Using Routed Event

Finished Event


If you don't want to bother writing it yourself, you may download my routed event snippet here.  I plan to create a code snippet for Dependency Properties that have coercion, event changed notification, and validation methods implemented so they may be overridden in descendent classes.

Filed under: , ,


# re: Routed Event Code Snippet

Friday, October 17, 2008 1:03 PM by Nathan Zaugg

I hope to also add the following snippits soon:

aevent - Attached Event

rcommand - Routed Command

dpropf - Dependency Property with full implementation of coercion, notification, and validation

vc - IValueConverter Implementation

Any other suggestions?

# re: Routed Event Code Snippet

Monday, October 27, 2008 5:17 PM by Nathan Zaugg

This is a good article, I think I'll probably reference it in an upcoming blog post.

# Uno snippet per i routed event

Thursday, August 19, 2010 10:25 AM by A DotNet Raider

Uno snippet per i routed event

# Routed Events - nowy rodzaj zdarze?? w WPF-ie - .NET Blog

Thursday, January 21, 2016 3:29 PM by Routed Events - nowy rodzaj zdarze?? w WPF-ie - .NET Blog

Pingback from  Routed Events - nowy rodzaj zdarze?? w WPF-ie - .NET Blog