Page view counter

Tip of the Day: Routed Events - Bubbling and not...

In Silverlight 2 Beta 1 some events bubble, and some do not. This can cause a bit of confusion, especially because the way this distinction is explained is itself confusing.

Bubbling

To set the stage, let's examine the following simple event handling program which is extracted from my forthcoming video on Event handling (Parts 1 and 2) that will be posted to our Silverlight 2 video page.

The example I'll be using is stripped down into a new application available here: TestRoutedEventsAndBubbling.zip

Keeping The Example Simple

To paraphrase A.E., an example should be as simple as possible, but no simpler.

This example requires a control (in this case a button) whose content is not text, but rather other controls. These concepts are covered in the first two tutorials on Silverlight 2.

We start by creating a new project and within that project a grid. The central control is a button whose Content is itself a StackPanel that contains 4 CheckBoxes

CheckBoxButton

The XAML for this is pretty straight forward and walked through in both the tutorials and the video,

ComplexButton

Notice in the XAML that we are looking at a single button that has a single content consisting of a stack panel within which are declared the four check boxes.

The question is this: when you click on the check boxes, will the button be clicked?

The answer is: it depends.  If you wire this up with a click event it will not bubble. Let's prove that.

ClickEvent

This code adds a Click Event to the button. Sure enough, if you click anywhere on the button except the check box, the button is clicked as reflected in the count in the text box, but if you click in the check box, the check box is set or cleared but the count is unaffected.

ButtonWithClickCount

Now, when I check the documentation, I'm left a bit puzzled, after all, it does say that click is a Routed Event,

ClickEventIsRouted

And it does seem to suggested that Routed Events are, well, routed...

RoutedEvent

The key is that there is routing and there is bubbling.

Now, to make this a bit more confusing, you may read an explanation that goes something like this....

"User code cannot created routed events which the Button is doing"

Eh? User code? Button?

The key thing to realize is that from the perspective of the folks who wrote the code handling routing, the Button is user code (!) just like any control you or I would write. A sharp distinction is made between the fundamental events such as MouseLeftButtonDown which is an event on Implement and which does bubble

MouseLeftDown

You can prove this to yourself by modifying your code only slightly. Comment out the click event and replace it with a MouseLeftButtonUp event handler

MouseLeftButtonUpEvent

When you run it now, it works just as before except that clicking the check box also clicks the button.

Published 21 March 2008 11:28 AM by jesseliberty

Comments

# Jesse Liberty - Silverlight Geek said on 21 March, 2008 01:06 PM

  So, after some minutes of trying this and that, it is time to take stock and see what's working

# Blogs said on 21 March, 2008 01:08 PM

  So, after some minutes of trying this and that, it is time to take stock and see what's working

# Mirrored Blogs said on 21 March, 2008 01:28 PM

  So, after some minutes of trying this and that, it is time to take stock and see what's working

# RajeevGoel said on 21 March, 2008 02:38 PM

After reading and rereading this post, I'm left still confused, and not really understanding the difference between routing and bubbling.  You've shown two examples, one which works and one which doesn't, and never explained the reasoning or the mechanism at play here.  There's also the impact of setting the Handled=true property on the event args, which I believe is related to this whole mess.  This topic deserves a more in-depth treatment.

# Blog Jocky » Blog Archive » Innovation, Renovation and Change said on 21 March, 2008 02:49 PM

Pingback from  Blog Jocky  » Blog Archive   » Innovation, Renovation and Change

# Innovation, Renovation and Change | My Geek Solutions said on 21 March, 2008 03:34 PM

Pingback from  Innovation, Renovation and Change | My Geek Solutions

# rob.eisenberg said on 21 March, 2008 04:20 PM

I have to admit, this is a mistake on the part of Microsoft's implementation of Silverlight and it *must* be fixed.  This is completely different from the way things work in WPF.  In WPF bubbling/tunneling are types of routing and events that behave in this way are accomplished the same way in "user code" as in the framework itself.  In WPF Button.Click is a true routed event and thus works differently than it does in SL; its actually intuitive because of the consistency.  With these types of differences between WPF and SL, Microsoft is setting itself and its customers up for a world of pain.  Speaking from the perspective of someone who has quite a bit of experience with WPF, I experience massive amounts of frustration trying to "port" my skills to SL.  You can read more about a very small set of the issues here:devlicio.us/.../there-s-some-darkness-in-your-silver-light.aspx

# Innovation, Renovation and Change | My Geek Solutions | My Geek Solutions said on 21 March, 2008 05:22 PM

Pingback from  Innovation, Renovation and Change | My Geek Solutions | My Geek Solutions

# re: Tip of the Day: Routed Events - Bubbling and not… | My Geek Solutions said on 21 March, 2008 05:22 PM

Pingback from  re: Tip of the Day: Routed Events - Bubbling and not… | My Geek Solutions

# Blog Jocky ?? Blog Archive ?? Innovation, Renovation and Change | My Geek Solutions said on 21 March, 2008 05:22 PM

Pingback from  Blog Jocky ?? Blog Archive ?? Innovation, Renovation and Change | My Geek Solutions

# re: Tip of the Day: Routed Events - Bubbling and not??? | My Geek … | My Geek Solutions said on 21 March, 2008 06:40 PM

Pingback from  re: Tip of the Day: Routed Events - Bubbling and not??? | My Geek … | My Geek Solutions

# Blog Jocky ?? Blog Archive ?? Innovation, Renovation and Change … | My Geek Solutions said on 21 March, 2008 06:40 PM

Pingback from  Blog Jocky ?? Blog Archive ?? Innovation, Renovation and Change … | My Geek Solutions

# Innovation, Renovation and Change | My Geek Solutions | My Geek … | My Geek Solutions said on 21 March, 2008 06:40 PM

Pingback from  Innovation, Renovation and Change | My Geek Solutions | My Geek … | My Geek Solutions

# Community Blogs said on 21 March, 2008 07:57 PM

Shawn Wildermuth creates Linkable SL Apps, Scott Morrison on Getting Started with SL2 and a walk-thru

# Blog Jocky ?? Blog Archive ?? Innovation, Renovation and Change … | Create a Blog said on 21 March, 2008 08:27 PM

Pingback from  Blog Jocky ?? Blog Archive ?? Innovation, Renovation and Change … | Create a Blog

# Blog Jocky ?? Blog Archive ?? Innovation, Renovation and Change | Create a Blog said on 21 March, 2008 08:33 PM

Pingback from  Blog Jocky ?? Blog Archive ?? Innovation, Renovation and Change | Create a Blog

# Innovation, Renovation and Change | Create a Blog said on 21 March, 2008 08:33 PM

Pingback from  Innovation, Renovation and Change | Create a Blog

# re: Tip of the Day: Routed Events - Bubbling and not??? | My Geek … | My Geek Solutions said on 21 March, 2008 09:00 PM

Pingback from  re: Tip of the Day: Routed Events - Bubbling and not??? | My Geek … | My Geek Solutions

# jesseliberty said on 21 March, 2008 09:10 PM

>>After reading and rereading this post, I'm left still confused, and not really understanding the difference between routing and bubbling.  You've shown two examples, one which works and one which doesn't, and never explained the reasoning or the mechanism at play here.<<

Rajeev.

I'm sorry that I have left you confused. I will do a follow up to see if I can sort it out for you and others who might be confused as well. Thanks for the feedback.

>>There's also the impact of setting the Handled=true property on the event args, which I believe is related to this whole mess.  This topic deserves a more in-depth treatment.<<

I don't think Handled is an argument in Silverlight; I think you have that confused with WPF, but I will research further.

# Innovation, Renovation and Change | Create a Blog | Create a Blog said on 21 March, 2008 09:19 PM

Pingback from  Innovation, Renovation and Change | Create a Blog | Create a Blog

# Innovation, Renovation and Change | Create a Blog | My Geek Solutions said on 21 March, 2008 09:58 PM

Pingback from  Innovation, Renovation and Change | Create a Blog | My Geek Solutions

# Jesse Liberty - Silverlight Geek said on 21 March, 2008 10:04 PM

One of the readers of my earlier post on routing and bubbling was left confused about when events are

# Mirrored Blogs said on 21 March, 2008 10:31 PM

One of the readers of my earlier post on routing and bubbling was left confused about when events are

# More on Routing and Bubbling | Create a Blog said on 21 March, 2008 10:43 PM

Pingback from  More on Routing and Bubbling | Create a Blog

# Innovation, Renovation and Change | Create a Blog | My Geek Solutions | Create a Blog said on 21 March, 2008 10:43 PM

Pingback from  Innovation, Renovation and Change | Create a Blog | My Geek Solutions | Create a Blog

# Innovation, Renovation and Change | Create a Blog | Create a Blog | Create a Blog said on 21 March, 2008 10:43 PM

Pingback from  Innovation, Renovation and Change | Create a Blog | Create a Blog | Create a Blog

# Blogs said on 21 March, 2008 11:01 PM

One of the readers of my earlier post on routing and bubbling was left confused about when events are

# Innovation, Renovation and Change | Create a Blog | My Geek Solutions | My Geek Solutions said on 21 March, 2008 11:05 PM

Pingback from  Innovation, Renovation and Change | Create a Blog | My Geek Solutions | My Geek Solutions

# Blog Jocky » Blog Archive » More on Routing and Bubbling said on 21 March, 2008 11:23 PM

Pingback from  Blog Jocky  &raquo; Blog Archive   &raquo; More on Routing and Bubbling

# More on Routing and Bubbling | My Geek Solutions said on 21 March, 2008 11:35 PM

Pingback from  More on Routing and Bubbling | My Geek Solutions

# Innovation, Renovation and Change | Create a Blog | My Geek … | My Geek Solutions said on 22 March, 2008 12:56 AM

Pingback from  Innovation, Renovation and Change | Create a Blog | My Geek &#8230; | My Geek Solutions

# More on Routing and Bubbling | My Geek Solutions | My Geek Solutions said on 22 March, 2008 12:56 AM

Pingback from  More on Routing and Bubbling | My Geek Solutions | My Geek Solutions

# More on Routing and Bubbling | Create a Blog | Create a Blog said on 22 March, 2008 01:00 AM

Pingback from  More on Routing and Bubbling | Create a Blog | Create a Blog

# Innovation, Renovation and Change | Create a Blog | My Geek … | Create a Blog said on 22 March, 2008 01:00 AM

Pingback from  Innovation, Renovation and Change | Create a Blog | My Geek &#8230; | Create a Blog

# Blog Jocky ?? Blog Archive ?? More on Routing and Bubbling | Create a Blog said on 22 March, 2008 01:00 AM

Pingback from  Blog Jocky ?? Blog Archive ?? More on Routing and Bubbling | Create a Blog

# Innovation, Renovation and Change | Create a Blog | Create a Blog … | Create a Blog said on 22 March, 2008 01:00 AM

Pingback from  Innovation, Renovation and Change | Create a Blog | Create a Blog &#8230; | Create a Blog

# More on Routing and Bubbling | My Geek Solutions | My Geek Solutions | My Geek Solutions said on 22 March, 2008 02:46 AM

Pingback from  More on Routing and Bubbling | My Geek Solutions | My Geek Solutions | My Geek Solutions

# Innovation, Renovation and Change | Create a Blog | My Geek ??? | My … | My Geek Solutions said on 22 March, 2008 02:46 AM

Pingback from  Innovation, Renovation and Change | Create a Blog | My Geek ??? | My &#8230; | My Geek Solutions

# More on Routing and Bubbling | Create a Blog | Create a Blog … | Create a Blog said on 22 March, 2008 05:32 AM

Pingback from  More on Routing and Bubbling | Create a Blog | Create a Blog &#8230; | Create a Blog

# Dew Drop - March 22, 2008 | Alvin Ashcraft's Morning Dew said on 22 March, 2008 02:41 PM

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

# Michael Sync » Silverlight 2 beta1 - Best of SilverlightCream said on 29 June, 2008 01:03 PM

Pingback from  Michael Sync  &raquo; Silverlight 2 beta1 - Best of SilverlightCream