Page view counter

Tip of the Day - Routed Events

Source Code

A routed event is is bubbled up the user interface tree whether or not it is handled by the object that receives the event. For example, if we return to a previous example in which we had a button within which were three sub-controls, the middle of which was a stack panel containing a  check box whose content had another stack panel with two buttons, the first of which itself had content which was an html button...

OneBusyButton

you can see that we want to be able to click on the radio button within the check box that is within the button and yet still have the click event bubble up and click the button. Yet, at the same time, we may want to respond to the click on the radio button or the check box being clicked. We saw the XAML for creating this control previously so there is no need to review that. It will be unchanged except that  we'll want to add just one control for this example, and that is a list box that can hold messages indicating which control has registered the mouse down event,

MessageListBox

Note that to accommodate this helpful list box we need to add a row and a column to the grid, which is easily accomplished in our row and grid definitions at the top of the file,

NewRowAndColumn

With that in place, we need only create event handlers to see how the events "bubble up" even as they are handled.

BubblingEventHandlers

Each of these event handlers is implemented to add a notice to the list box we added in the third column,

image

If you click on an outer object (such as the check box border, only one event fires,

OneEventFired

Whereas if you click on an inner object (e.g., a radio button, you can see that the event is routed to the outer (containing) objects for handling as well.

clickonRadioButton

In this image, the user has clicked on the inner radio button. Because MouseLeftDown is a routed event, the event bubbles up through the UI tree, from the radio button to the check box to the surrounding border (and on up through the other surrounding UI elements that don't have event handlers!)

Published 10 March 2008 10:00 AM by jesseliberty

Comments

# wisecarver said on 10 March, 2008 11:21 AM

Thanks Jesse...

Love the way the ListBox has properties for the Grid. ;-)

# Qbus said on 10 March, 2008 04:45 PM

Nice illutration of this event-bubbling :)

What if you want to avoid this from happening? Then you use the "e.Handled = true;" on the EventArgs in the events?

Then what will happen? The event will start bubbling?

I tryed this out, only a very wuick test, so maybe I did something wrong, but it didn't seemed to work, so that's why I'm asking here.

:)

# Bydia3 said on 10 March, 2008 08:59 PM

Can I catch an event at the root of the tree and still know what was the original object the event started on like in html?  Why? I'd like to have a method handle all my events rather than have to wire-up every object that needs it.

# BenHayat said on 10 March, 2008 10:59 PM

>>What if you want to avoid this from happening? Then you use the "e.Handled = true;" on the EventArgs in the events?

Then what will happen? The event will start bubbling?

I tryed this out, only a very wuick test, so maybe I did something wrong, but it didn't seemed to work, so that's why I'm asking here.<<

In the TOP event handler, you now need to check if "!e.Handled==true" then perform something else if it is handled by lower event handlers (e.Handled==true) then don't do anything. So a check must be added now!

..Ben

# Dew Drop - March 11, 2008 | Alvin Ashcraft's Morning Dew said on 11 March, 2008 08:36 AM

Pingback from  Dew Drop - March 11, 2008 | Alvin Ashcraft's Morning Dew

# Jesse Liberty - Silverlight Geek said on 16 March, 2008 09:14 PM

The Tip of the Day occasionally will delve deep into code (other days it will just provide a bit of useful

# Mirrored Blogs said on 16 March, 2008 09:41 PM

The Tip of the Day occasionally will delve deep into code (other days it will just provide a bit of useful

# Blogs said on 16 March, 2008 09:41 PM

The Tip of the Day occasionally will delve deep into code (other days it will just provide a bit of useful

# Wöchentliche Rundablage: ASP.NET MVC, Silverlight 2, LINQ… | Code-Inside Blog said on 17 March, 2008 04:11 PM

Pingback from  W&ouml;chentliche Rundablage: ASP.NET MVC, Silverlight 2, LINQ&#8230; | Code-Inside Blog