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