Two Part Video on Event Handling

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)

EventHandling

 

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

CreateButton

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.

Share

About Jesse Liberty

Jesse Liberty is an independent consultant and programmer with three decades of experience writing and delivering software projects. He is the author of 2 dozen books and multiple Pluralsight courses, and has been a Senior Technical Evangelist for Microsoft, a Distinguished Software Engineer for AT&T, a VP for Information Services for Citibank and a Software Architect for PBS. He is a Xamarin Certified Mobile Developer and a Xamarin MVP, Microsoft MVP and Telerik MVP.
This entry was posted in z Silverlight Archives. Bookmark the permalink.

Leave a Reply

Your email address will not be published.