Jesse Liberty - Silverlight Geek

By, For and About Silverlight Developers

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

Comments

wisecarver said:

Thanks Jesse...

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

# March 10, 2008 11:21 AM

Qbus said:

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.

:)

# March 10, 2008 4:45 PM

Bydia3 said:

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.

# March 10, 2008 8:59 PM

BenHayat said:

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

# March 10, 2008 10:59 PM

Dew Drop - March 11, 2008 | Alvin Ashcraft's Morning Dew said:

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

# March 11, 2008 8:36 AM

Jesse Liberty - Silverlight Geek said:

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

# March 16, 2008 9:14 PM

Mirrored Blogs said:

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

# March 16, 2008 9:41 PM

Blogs said:

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

# March 16, 2008 9:41 PM

Wöchentliche Rundablage: ASP.NET MVC, Silverlight 2, LINQ… | Code-Inside Blog said:

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

# March 17, 2008 4:11 PM