Jesse Liberty - Silverlight Geek

By, For and About Silverlight Developers

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.

Comments

Jesse Liberty - Silverlight Geek said:

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

# March 21, 2008 1:06 PM

Blogs said:

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

# March 21, 2008 1:08 PM

Mirrored Blogs said:

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

# March 21, 2008 1:28 PM

RajeevGoel said:

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.

# March 21, 2008 2:38 PM

Blog Jocky » Blog Archive » Innovation, Renovation and Change said:

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

# March 21, 2008 2:49 PM

Innovation, Renovation and Change | My Geek Solutions said:

Pingback from  Innovation, Renovation and Change | My Geek Solutions

# March 21, 2008 3:34 PM

rob.eisenberg said:

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

# March 21, 2008 4:20 PM

Innovation, Renovation and Change | My Geek Solutions | My Geek Solutions said:

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

# March 21, 2008 5:22 PM

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

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

# March 21, 2008 5:22 PM

Blog Jocky ?? Blog Archive ?? Innovation, Renovation and Change | My Geek Solutions said:

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

# March 21, 2008 5:22 PM

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

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

# March 21, 2008 6:40 PM

Blog Jocky ?? Blog Archive ?? Innovation, Renovation and Change … | My Geek Solutions said:

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

# March 21, 2008 6:40 PM

Innovation, Renovation and Change | My Geek Solutions | My Geek … | My Geek Solutions said:

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

# March 21, 2008 6:40 PM

Community Blogs said:

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

# March 21, 2008 7:57 PM

Blog Jocky ?? Blog Archive ?? Innovation, Renovation and Change … | Create a Blog said:

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

# March 21, 2008 8:27 PM

Blog Jocky ?? Blog Archive ?? Innovation, Renovation and Change | Create a Blog said:

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

# March 21, 2008 8:33 PM

Innovation, Renovation and Change | Create a Blog said:

Pingback from  Innovation, Renovation and Change | Create a Blog

# March 21, 2008 8:33 PM

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

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

# March 21, 2008 9:00 PM

jesseliberty said:

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

# March 21, 2008 9:10 PM

Innovation, Renovation and Change | Create a Blog | Create a Blog said:

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

# March 21, 2008 9:19 PM

Innovation, Renovation and Change | Create a Blog | My Geek Solutions said:

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

# March 21, 2008 9:58 PM

Jesse Liberty - Silverlight Geek said:

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

# March 21, 2008 10:04 PM

Mirrored Blogs said:

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

# March 21, 2008 10:31 PM

More on Routing and Bubbling | Create a Blog said:

Pingback from  More on Routing and Bubbling | Create a Blog

# March 21, 2008 10:43 PM

Innovation, Renovation and Change | Create a Blog | My Geek Solutions | Create a Blog said:

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

# March 21, 2008 10:43 PM

Innovation, Renovation and Change | Create a Blog | Create a Blog | Create a Blog said:

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

# March 21, 2008 10:43 PM

Blogs said:

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

# March 21, 2008 11:01 PM

Innovation, Renovation and Change | Create a Blog | My Geek Solutions | My Geek Solutions said:

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

# March 21, 2008 11:05 PM

Blog Jocky » Blog Archive » More on Routing and Bubbling said:

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

# March 21, 2008 11:23 PM

More on Routing and Bubbling | My Geek Solutions said:

Pingback from  More on Routing and Bubbling | My Geek Solutions

# March 21, 2008 11:35 PM

Innovation, Renovation and Change | Create a Blog | My Geek … | My Geek Solutions said:

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

# March 22, 2008 12:56 AM

More on Routing and Bubbling | My Geek Solutions | My Geek Solutions said:

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

# March 22, 2008 12:56 AM

More on Routing and Bubbling | Create a Blog | Create a Blog said:

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

# March 22, 2008 1:00 AM

Innovation, Renovation and Change | Create a Blog | My Geek … | Create a Blog said:

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

# March 22, 2008 1:00 AM

Blog Jocky ?? Blog Archive ?? More on Routing and Bubbling | Create a Blog said:

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

# March 22, 2008 1:00 AM

Innovation, Renovation and Change | Create a Blog | Create a Blog … | Create a Blog said:

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

# March 22, 2008 1:00 AM

More on Routing and Bubbling | My Geek Solutions | My Geek Solutions | My Geek Solutions said:

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

# March 22, 2008 2:46 AM

Innovation, Renovation and Change | Create a Blog | My Geek ??? | My … | My Geek Solutions said:

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

# March 22, 2008 2:46 AM

Innovation, Renovation and Change | Create a Blog | My Geek ??? | My … | Create a Blog said:

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

# March 22, 2008 3:14 AM

More on Routing and Bubbling | Create a Blog | Create a Blog | Create a Blog said:

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

# March 22, 2008 3:14 AM

Blog Jocky ?? Blog Archive ?? More on Routing and Bubbling … | Create a Blog said:

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

# March 22, 2008 3:14 AM

More on Routing and Bubbling | My Geek Solutions | My Geek … | My Geek Solutions said:

Pingback from  More on Routing and Bubbling | My Geek Solutions | My Geek &#8230; | My Geek Solutions

# March 22, 2008 4:46 AM

More on Routing and Bubbling | Create a Blog | Create a Blog … | Create a Blog said:

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

# March 22, 2008 5:32 AM

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

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

# March 22, 2008 2:41 PM