I'm pleased to announce a two part How Do I video set on Event Handling in Silverlight 2 Beta 1. These videos explore how to hook up events and point out that in Silverlight some events "bubble" and some do not.
Here are the key points to walk away with
- You can set the event handler in XAML, but I don't, and I personally don't think it is a good practice, though that is not an official Microsoft position
<Button x:Name="Submit" Click="Submit_Click" />
- The alternative to setting the event handler in XAML is to set it in the Code Behind file (e.g., Page.XAML.cs)
- In either case Intellisense will help with the creation of the event and will create the skeleton of the implementation
- Some events "bubble" up through the interface tree (and some do not)
How Bubbling Works – The 60 Second Version
Suppose I create a button with four check boxes in it,
Note that the button is named SetFeatures , and within that Button is a stack panel containing 4 named CheckBoxes. I do not need event handlers for the 4 check boxes; only the button, but if I want clicking on the check boxes also to be counted as a click on the button, the event I must wire up is OnMouseLeftButtonUp and not Button.Click!
SetFeatures.MouseLeftButtonUp += new MouseButtonEventHandler(SetFeatures_MouseLeftButtonUp);
It turns out that many of the Mouse events do bubble but none of the control events (such as click) bubble, at least in Beat 1.
Because MouseLeftButtonUp does bubble, it is passed up to the Button after it is handled by the checkbox; but click would be handled by the check box and then eaten.
This is shown in detail in the two videos.