Bookmark and Share Share...    Subscribe to this feed Feed   About Christian Moser  

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));

Last modified: 2010-02-08 17:52:53
Copyright (c) by Christian Moser, 2011.

 Comments on this article

Show all comments
Commented on 10.June 2009
perhaps this might be over-simplified...
Commented on 26.June 2009
some aditonal info would be nice
Commented on 6.July 2009
Please provide some more examples and explanations
party drunk...
Commented on 20.July 2009
ehh... thanks )
LC Druid
Commented on 16.August 2009
Now I can add "WPF Routed Events Mastah" to my resume. It'll go well with "Love Bandit" entry that's already there.
alan huffman
Commented on 20.August 2009
You're a douchebag for having created a overly simplistic example that has ZERO value and you made it to google's #1 pick. Well done.
Commented on 7.September 2009
well, as noted, this is overly simplistic, and needs more examples on how to use each type of routed event.
Commented on 26.October 2009
>> alan huffman : So lets kill him for that. As they do with "witches". Burn him, torture him !! .... asshole. Alan, your poor existence has zero value.
Commented on 29.October 2009
Once upon a time we hired a multi book author only to find he compiled books from knowledge he gleaned from others but didn't understand himself. Sad. You'd think someone besides us would have noticed. I've been in this business for > 20 years and been through every sort of MS Hell along the way. Your example may be great for someone that has a good start with WPF but you are writing a "how to" article for beginners. Or that's what you make it appear. Simpler? Please?
Commented on 29.October 2009
Nice one. if anyone liks to know more about "Routed Events" try this link
Commented on 31.October 2009
why does msft comes up with a new technology every new month... it sucks
Christian Moser
Commented on 1.November 2009
Hi msftsucks,

Microsoft does really invest a lot in the new XAML programming techniques (WPF and Silverlight). Sooner or later it will also be included in Windows Mobile. Even the Java community talks about using XAML to describe user interfaces. So I think this time it's worth learning the new concepts, because they will get the new standard.

bob bani
Commented on 3.November 2009
Hi msftsucks,

why does BMW come up with new cars? why do apple, DELL,... come up with new products?
THINK first then talk!
Commented on 4.November 2009
More code would have been better. Suppose there is a Button inside a button, how can i use routed event for the inner buuton...

This site is too coool for a beginer.... thnaks...
Commented on 18.November 2009
This site decribes more in details for routed events..
Commented on 26.November 2009
it would be nice to have more detail here... i\'m still stumbling with usage
Commented on 30.December 2009
what are the use of these different events? direct raising isn\\\'t cool enough?
Commented on 4.January 2010
hey its really cool...
can u tell me hw i do the same in
Commented on 6.January 2010
Anyone know how to do this in, i've been stuck on this for days? Anyone?
Commented on 3.February 2010
Needs some expalanation as to what this is used for and examples.
Commented on 4.February 2010
The routing strategy can be bubble, tunnel or direct. plz give some example on tihs .....or make clear these point.........
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.
Commented on 16.February 2010
Really great article, thks a lot. Don't pay attention to stupid opinions.
Commented on 19.February 2010
this is very easy to learn wpf application
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.
Commented on 1.March 2010
Ever, right on!
Small step indeed.
Very helpfull, thank you.
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!
Commented on 9.March 2010
Can you explain the routed event on DependencyObject.
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.
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.
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.
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:
The answer is: Evolution
Commented on 30.June 2010
I just needed a quick refresher and for that this was excellent. Thanks.
Commented on 30.June 2010
I just needed a quick refresher and for that this was excellent. Thanks.
Commented on 30.June 2010
I just needed a quick refresher and for that this was excellent. Thanks.
Commented on 30.June 2010
I just needed a quick refresher and for that this was excellent. Thanks.
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?
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).
Commented on 10.July 2010
I agree Branko you are correct
Commented on 9.August 2010
Not to mention that there's no such thing as PreviewClick.
Commented on 16.August 2010
i expect explaination on code
Commented on 23.August 2010
wow you guys are really that impressed by mosers copy and pasting
Commented on 7.September 2010
See event bubbling in a custom ASP.NET control.

If you want to see the built-in action, just define the same event handler for all children of an element for MouseDown

//Window contructor
this.MouseDown += MouseDownHandler;
myBorder.MouseDown += MouseDownHandler;
myPanel.MouseDown += MouseDownHandler;
myEllipse.MouseDown += MouseDownHandler;
myRectangle.MouseDown += MouseDownHandler;

void MouseDownHandler(object sender, MouseButtonEventArgs e)
Debug.WriteLine("MouseDown: " + sender);
//this means that you handled the event and bubbling stops at the
// child that raised the event
//e.Handled = true;

Oh and can someone do something about this CommentTextBox. It's small and drives me up the wall. And the fontsize is hurting my eyes. Yes, I know I could fix it from my end, but it would be nice if i didn't have to :)
Anand Rajak
Commented on 13.September 2010
great thanks.......
Commented on 16.September 2010
I don't know if i am missing something here but just to make sure, can you clarify this.

The main difference between routedevents and the old .net2.0 version of event propagation is that this version does not necessarily have to be bound to an event handler before RaiseEvent can be called?

Commented on 16.September 2010
I don't know if i am missing something here but just to make sure, can you clarify this.

The main difference between routedevents and the old .net2.0 version of event propagation is that this version does not necessarily have to be bound to an event handler before RaiseEvent can be called?

Jothimnai V
Commented on 28.October 2010
What is the Use of the Routed Event in WPF or Silverlight. Why can't we achive the same using ordinary event
Commented on 28.December 2010
nice description
Commented on 12.January 2011
Man, I must say your website is the best in explaining WPF concepts. MSDN sucks & all the books are just too long to read!
Akshay Bharde
Commented on 15.February 2011
Bubbling for MouseDown event doesn't work for usual controls: buttons, textbox, etc. On the other hand PreviewMouseDown works for the same. Also, bubbling for MouseDown works for container controls like Windows, StackPannel, Grid, etc. Why is it so? Pasting the XAML for your reference:

<Window x:Class="RoutedEvents.Window1" MouseDown="Window_MouseDown" PreviewMouseDown="Window_PreviewMouseDown"
Title="Window1" Height="300" Width="300">
<StackPanel x:Name="grdNew" MouseDown="grdNew_MouseDown" PreviewMouseDown="grdNew_PreviewMouseDown" Background="Cornsilk" Height="187" Width="221">
<Button x:Name="btnNew" MouseDown="btnNew_MouseDown" PreviewMouseDown="btnNew_PreviewMouseDown" Height="84" Background="AliceBlue" MouseUp="btnNew_MouseUp">
<TextBox x:Name="txtNew" MouseDown="txtNew_MouseDown" PreviewMouseDown="txtNew_PreviewMouseDown" Width="103"></TextBox>
sanjay patoliaa
Commented on 26.March 2011
Nice one but should have one proper example to get clear practical understanding of all the routing strategy
Commented on 15.April 2011
the tutorial comes handy to a new WPF programmer like me, by keeping the subject terse but well touching most of the points needed for a beginner. Hope Christian will extend the site soon for a next level [for an advanced programmer too]. Good Job. Chakkanaina article. Azagaana article.
Commented on 12.May 2011
Nice one but Need more practical example to get clear understanding.
Commented on 5.June 2011
To make these tutorials, even more useful, give some downloadable simple example.
Commented on 7.June 2011
Best for quick look at basic concepts and understanding of WPF. Covering in depth of all concepts with an example will make this site excellent
Om Prakash
Commented on 17.June 2011
really great.
Commented on 6.July 2011
Really Very good

E-Mail (optional)