Bookmark and Share Share...    Subscribe to this feed Feed   About me...


Routed Events

Routed events are events which navigate up or down the visual tree acording to their RoutingStrategy. The routing strategy can be bubble, tunnel or direct. You can hook up event handlers on the element that raises the event or also on other elements above or below it by using the attached event syntax: Button.Click="Button_Click".

Routed events normally appear as pair. The first is a tunneling event called PreviewMouseDown and the second is the bubbling called MouseDown. They don't stop routing if the reach an event handler. To stop routing then you have to set e.Handled = true;

  • Tunneling The event is raised on the root element and navigates down to the visual tree until it reaches the source element or until the tunneling is stopped by marking the event as handeld. By naming convention it is called Preview... and appears before corresponding bubbling event.

  • Bubbling The event is raised on the source element and navigates up to the visual tree until it reaches the root element or until the bubbling is stopped by marking the event as handled. The bubbling event is raised after the tunneling event.

  • Direct The event is raised on the source element and must be handled on the source element itself. This behavior is the same as normal .NET events.

How to Create a Custom Routed Event

 
 
// Register the routed event
public static readonly RoutedEvent SelectedEvent = 
    EventManager.RegisterRoutedEvent( "Selected", RoutingStrategy.Bubble, 
    typeof(RoutedEventHandler), typeof(MyCustomControl));
 
// .NET wrapper
public event RoutedEventHandler Selected
{
    add { AddHandler(SelectedEvent, value); } 
    remove { RemoveHandler(SelectedEvent, value); }
}
 
// Raise the routed event "selected"
RaiseEvent(new RoutedEventArgs(MyCustomControl.SelectedEvent));
 
 
 



 Comments on this article

Show all comments
JasonK
Commented on 9.February 2010
I'll second the requests for examples of how or when you would need to use this. Conceptually it's hard to see the need for this or determine when it would really be helpful.
Ever
Commented on 16.February 2010
Really great article, thks a lot. Don't pay attention to stupid opinions.
prafull
Commented on 19.February 2010
this is very easy to learn wpf application
Bolli
Commented on 24.February 2010
If i didn't already know, im not sure if i could see the point of routed events by reading this article. Explaining that and examples of not only correct syntax but a useful application would be really helpful.
RogerL
Commented on 1.March 2010
Ever, right on!
Small step indeed.
Very helpfull, thank you.
Mike
Commented on 5.March 2010
I don't know what you all complain about... the chart and the code samples perfectly explain the possibilities of routed events...
thanks for that!
Pardo
Commented on 9.March 2010
Can you explain the routed event on DependencyObject.
Jeremy
Commented on 18.March 2010
Once again, to everyone complaining; this isn't a hand-holding tutorial. If you aren't professional enough to fill in the blanks on your own, you have NO business being a developer. I read this twice, googled around for 20 minutes, read it again, and wrote a small test application using the concepts. You guys spend more time complaining then learning. Sheesh.
nahid
Commented on 21.March 2010
It is good tutorial because it is SIMPLE! Just feel the power of simplicity .
Steven Woff
Commented on 1.April 2010
Great sample, sometimes all you need is a snippet of code to copy and paste and to move on. This example is exactly that - a perfect sample for embedding into my code - with a little bit of background to make sure you are on the right track. Found this in Google, pasted it into my user control, wired up the events and problem solved - thanks Christian!
Very cool
Commented on 2.April 2010
I am loving this tutorial, this is very simple as compared to all other sources I came across.
kevin
Commented on 6.April 2010
well done. very concise!
Simon Rückert
Commented on 10.April 2010
thank you, its very helpful to understand the wtf concepts.
Harris
Commented on 23.May 2010
@bob bani - Coming up with new car is entirely different from coming up with a new concept, at least if a new car comes, it has same way of driving, applying brakes, etc.

THINK first then talk!
Elvis N.
Commented on 2.June 2010
I think a more "reasonable" answer can be found here:
http://oreilly.com/news/graphics/prog_lang_poster.pdf
The answer is: Evolution
Basarat
Commented on 30.June 2010
I just needed a quick refresher and for that this was excellent. Thanks.
Basarat
Commented on 30.June 2010
I just needed a quick refresher and for that this was excellent. Thanks.
Basarat
Commented on 30.June 2010
I just needed a quick refresher and for that this was excellent. Thanks.
Basarat
Commented on 30.June 2010
I just needed a quick refresher and for that this was excellent. Thanks.
Mako
Commented on 6.July 2010
Every man and his bloody dog have written "tutorials" on this, and not ONE shows you how to do it. Bubbling goes up, tunneling goes down. We get it. SHOW US THE FLAMING CODE. What do I declare and where? If I have a main screen with a tabcontrol whose tabitems contain usercontrol forms, and I have a button on one that should switch to another tab, how could I do it?
Branko
Commented on 9.July 2010
The diagram is incorrect. Assuming we are talking about Click event on Button, then the PreviewClick (tunneling) arrow should go from Window to Button (not from Button downwards).
Dadi
Commented on 10.July 2010
I agree Branko you are correct
tb
Commented on 9.August 2010
Not to mention that there's no such thing as PreviewClick.
Mallesh
Commented on 16.August 2010
i expect explaination on code
thedirty
Commented on 23.August 2010
wow you guys are really that impressed by mosers copy and pasting

Name
E-Mail (optional)
Comment
About Christian Moser