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" />
</StackPanel>
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
- Find a snippet you plan to modify in the Manage Snippets Dialog (Tools -> Code Snippets Manager)
- 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
- Edit the declarations (like):
<Literal>
<ID>name</ID>
<ToolTip>Event Name</ToolTip>
<Default>MyEvent</Default>
</Literal>
- Edit the snippet using $name$ for any literal replacement.
- Be sure to change the shortcut in the XML to "revent".
- Save the file as "revent.snippet"
- 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="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>Define a RoutedEvent</Title>
<Shortcut>revent</Shortcut>
<Description>Code snippet for a event using RoutedEvent</Description>
<Author>Nathan Zaugg</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>eventhandlertype</ID>
<ToolTip>Event Handler Type Type</ToolTip>
<Default>RoutedEventHandler</Default>
</Literal>
<Literal>
<ID>name</ID>
<ToolTip>Event Name</ToolTip>
<Default>MyEvent</Default>
</Literal>
<Literal>
<ID>ownerclass</ID>
<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:
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.
Links: