Page view counter

More on Routing and Bubbling

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

The documentation is clearer about this in some places than in others. For example, the Mouse events documentation has this to say:

MouseEventsAndBubbling

There are two things to notice here. One is that this particular documentation does clearly distinguish those events that bubble from those that do not.

The second is that the first sentence unfortunately seems to equate being a routed event with bubbling. What I think it means to say is "are routed events that bubble" 

The documentation shows that MouseEnter (which does not bubble) is an event of type MouseEventHandler

MouseEnterEvent

and MouseEventHandler is a delegate for three non bubbling events:

MouseEventHandler

So.... we can safely conclude that

  • All bubbling events are routed events but
  • Not all routed events bubble

( All C# Programmers are human, but not all humans are C# Programmers * )

So which events Do and Do Not Bubble and Why?

One rule seems to be that any event specific to a control does not bubble. That means that Button.Click and Slider.ValueChanged do not bubble.

Wait a minute! What would it mean for Slider.ValueChanged to bubble? Where would it bubble to? What could contain a slider that could possibly do anything with the ValueChanged event?  Clearly that event needs to go to the Slider and nowhere else.

Okay, what about a ListBox which has SelectionChanged. Where should that bubble to?

We're beginning to see the pattern. Most of the time, it doesn't make sense for the events specific to a given control to bubble.

(Clearly not all of the time - Click is a good example where you might want a control event to bubble,  but you can work around that as we did in the previous example with mouse up.)

In WPF you can decide because there are three types of Routed Events (Bubbling, Direct and Tunneling). In Silverlight, for Beta 1 at least, what has been put in place is that control-specific events do not bubble and what does bubble is many (though not all) of the more fundamental events  such as MouseDown/Up/Move,  which lets you get what you want, though not always as elegantly.

One of the other comments was that this "must be fixed."  I'll post my own speculations on that tomorrow, but whether or not this is changed, a few things are becoming clearer:

  • It is possible to accomplish all the event handling you might want as implemented
  • There certainly will be some confusion for WPF programmers who have a superset of the capabilities of Silverlight
  • We need to get our terminology straight and consistent

I hope this clarifies any remaining areas of confusion but if not, please do leave more comments and we'll see if we can't sort it out.

Thanks!

-jesse

* - Yes, yes, some computers no doubt write C# programs and I bet you've met some C# programmers about whom you've had your doubts.

Published Friday, March 21, 2008 10:04 PM by jesseliberty

Comments

# re: More on Routing and Bubbling

>>All C# Programmers are human

Weeeeeel, I'm not sure if I agree with that 100% ;-)

Friday, March 21, 2008 10:29 PM by BenHayat

# re: More on Routing and Bubbling | My Geek Solutions

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

Friday, March 21, 2008 11:30 PM by re: More on Routing and Bubbling | My Geek Solutions

# Routing and Bubbling

Over at Jesse Liberty 's Blog , he has a great explanation of Routed Events and Bubbling (what Bubbles

Saturday, March 22, 2008 6:29 AM by Techniques

# Routing and Bubbling | My Geek Solutions

Pingback from  Routing and Bubbling | My Geek Solutions

Saturday, March 22, 2008 7:29 AM by Routing and Bubbling | My Geek Solutions

# Routing and Bubbling | My Geek Solutions | My Geek Solutions

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

# Routing and Bubbling | My Geek Solutions | My Geek Solutions | My Geek Solutions

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

# Routing and Bubbling | My Geek Solutions | My Geek Solutions | My … | My Geek Solutions

Pingback from  Routing and Bubbling | My Geek Solutions | My Geek Solutions | My … | My Geek Solutions

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

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

Saturday, March 22, 2008 2:41 PM by Dew Drop - March 22, 2008 | Alvin Ashcraft's Morning Dew

# Silverlight Cream for March 23, 2008 -- #232

Rob Houweling tackles unpacking ZIP files in SL2, Marlon Grech on the DataGrid in Blend, Jesse Liberty

Sunday, March 23, 2008 8:32 PM by Community Blogs

# re: More on Routing and Bubbling

"What could contain a slider that could possibly do anything with the ValueChanged event?"

Easy: what if I want a Panel to have a generic ValueChanged handler for all the sliders it contains? Without bubbling, I need to register all slider event by hand. This is easy when the Panel adds itself the slider to itself, but what if an element is added to the Panel? I then need to scan the visual tree to retrieve all the sliders inside it.

Clearly, if WPF does it, Silverlight should do it also. I think bubbling has been almost stripped from Silverlight to keep the plugin as lightweight as possible.

Thursday, February 26, 2009 9:11 AM by dummyboy

# re: More on Routing and Bubbling

I've seen too many of these bloggers make such false claims and conclusions.  " What would it mean for Slider.ValueChanged to bubble? ... Clearly that event needs to go to the Slider and nowhere else"

Thanks dummyboy, for showing the way to such folks.

Wednesday, November 04, 2009 2:12 PM by Marie123