All posts

Routed Events in Silverlight 2

Introduction

Hi all, my name is Hristo Hristov and I’m a developer on Telerik’s Silverlight team. This is my first blog post and I’d like to say a few words about Routed Events and Silverlight.

As probably all of you know, Silverlight supports Routed Events. However, there is a catch – currently (Beta 2) just a few core events are routed and developers are not allowed to create their own Routed Events. This is quite unfortunate because Routed Events are a very powerful mechanism that helps you write more concise and elegant code.

To help you overcome this limitation, we introduced in RadControls for Silverlight 2 the ability to create and use Routed Events. If you have WPF background, you will be surprised to see the EventManager, RoutedEvent and RoutingStrategy types in our controls. We’ve taken care to mirror exactly the same functionality from their WPF counterparts, so using them in both Silverlight and WPF is identical.

Basically, Routed Events traverse the logical tree upwards or downwards, depending on their RoutingStrategy – Bubble and Tunnel respectively. Most Routed Events have a corresponding CLR event that wraps them and allows developers to use the Routed Event as a standard CLR one, as well as to add handlers to it in XAML. Another interesting thing to know is that by WPF convention, all tunneling events are prefixed with Preview – e.g. PreviewExpand. For more in-depth information about Routed Events in WPF you can read this excellent article on MSDN: http://msdn.microsoft.com/en-us/library/ms742806(VS.85).aspx.

Almost all of the events in RadControls for Silverlight 2 are Routed Events. This gives you more freedom when you design your applications since you can write instance handlers as well as class handlers for our controls. For example, if you want to handle all RadMenuItem Click events you can write:

EventManager.RegisterClassHandler(typeof(TheClassThatWillHandleRadMenuItemsClik), RadMenuItem.ClickEvent, new RoutedEventHandler(OnMenuClick), true)   
 
private void OnMenuClick(object sender, RoutedEventArgs e)  
{  
   RadMenuItem menuItem = e.Source as RadMenuItem;  
}   
 

Keep in mind that adding class handler will keep a reference to the object that declares the handler. The recommended way is to use RegisterClassHandler method only in the static constructor.

The example is pretty self-explanatory, except the last parameter of the RegisterClassHandler method. What it does is to allow you to process an event that has already been handled, overriding the default routing behavior.

Another way to add handler that process handled events is to use AddHandler method and pass true as a last parameter.

While reading about Routed Events I found one very interesting demo from Karl Shifflett called Routed Event Viewer: http://karlshifflett.wordpress.com/2007/11/07/routed-event-viewer/. The demo visually demonstrates event routing and handling. You can see my Silverlight version of the demo below. The demo is using Telerik Rad Controls for Silverlight. Click on the "Start Demo" button to run the application:

Get Microsoft Silverlight

 

 

The Silverlight source code is available here: 

SilverlightRoutedEvents.zip,

the WPF version is available here:

http://karlshifflett.wordpress.com/2007/11/07/routed-event-viewer

Comments  5

  • 23 Jul, 06:27 AM

    Hi Hristo Hristov;

    Great, first blog!
    It's very nice that you guys put these educational blogs out there, especially coming from developers who are deep into SL and WPF. Please keep them coming.. It really adds a lot of credibility to telerik's position in this market!

    Thanks!

    comment by: Ben
  • 23 Jul, 02:27 PM

    Hi Hristo,

    Very nice - thank you!
    It would be really great to get the source.

    comment by: David
  • 24 Jul, 03:03 AM

    Hi Ben,
    Thanks for the good words. I'll write for all the goodies that we create here at Telerik.
    If you want to hear more on specific topic please tell us.

    Hi David,
    I have attached the source code of the demo (at the bottom of the post). You will need to add reference to Telerik.Windows.Controls in order to build it.
    The source code of the WPF version is available in the Karl post.

    comment by: Hristo
  • 27 Jul, 09:03 AM

    Hi Hristo;

    There is one area of SL that is kind of Gray and hopefully as time goes no, this will become clearer, but meanwhile, a company like telerik who is involved both in WPF and SL can help to clarify this area.
    When SL 1.0 started, it had certain features that were different than its big brother WPF, and as SL 2 came, it somewhat followed the pattern and structure of WPF. But this division has not become clear by MSFT and due to lack of docs for SL, sometime we have to resort to WPF docs for further reading. But this also raises other confusions and frustrations, because not all WPF and SL counterparts work the same, so you learn something in WPF, but it does not work in SL.
    I personally greatly appreciate the efforts telerik team has put together to create a uniform API for WPF and SL. This really saves a lot of time working with telerik products.
    However, this is still a problem with MSFT side. My request and suggestion, if your resources allow, is provide more blog and information (based on your own findings) on the subjects of where things are different between SL and WPF and where things are the SAME. For example of Data bindings, Control templates, data templates and so on.
    This is my humble request to address this issue as much as your resources allow. Thank you in advance!
    ..Ben

    comment by: Ben
  • 23 Aug, 05:47 PM

    Hi all. I saw pictures of live fish which was made by Hristo.I would like to know,what digital camera he used.
    Thank you for answers.
    My email; k_poT11@yahoo.com
    Best Regards.

    comment by: Mark
Post a comment!
  1. Security image