Input with Windows Presentation Foundation - Routed Events, continued
(Page 2 of 4 )
You may be wondering whether there is a meaningful difference between a direct routed event and an ordinary CLR event—after all, a direct event isn’t really routed anywhere. The main difference is that with a direct routed event, WPF provides the underlying implementation, whereas if you were to use the normal C# event syntax to declare an event, the C# compiler would provide the implementation. The C# compiler would generate a hidden private field to hold the event handler, meaning that you pay a per-object overhead for each event whether or not any handlers are attached. With WPF’s event implementation, event handlers are managed in such a way that you pay an overhead only for events to which handlers are attached. In a UI with thousands of elements each offering tens of events, most of which don’t have handlers attached, this starts to add up. Also, WPF’s event implementation offers something not available with ordinary C# events: attached events, which are described later.
With the exception of direct events, WPF defines most routed events in pairs—one bubbling and one tunneling. The tunneling event name always begins withPreviewand is raised first. This gives parents of the target element the chance to see the event before it reaches the child (hence thePreview prefix). The tunneling preview event is followed directly by a bubbling event. In most cases, you will handle only the bubbling event—the preview would usually be used only if you wanted to be able to block the event, or if you needed a parent to do something in advance of normal handling of the event.
In Example 4-1, most of the elements have event handlers specified for thePreviewMouseDownandMouseDownevents—the bubbling and tunneling events, respectively. Example 4-2 shows the corresponding code-behind file.
Example 4-2. Handling events
using System;
using System.Windows;
using System.Diagnostics;
namespace EventRouting {
public partial class Window1 : Window {
public Window1() {
InitializeComponent();
}
void PreviewMouseDownButton(object sender, RoutedEventArgs e)
{ Debug.WriteLine("PreviewMouseDownButton"); }
void MouseDownButton(object sender, RoutedEventArgs e)
{ Debug.WriteLine("MouseDownButton"); }
void PreviewMouseDownGrid(
object sender, RoutedEventArgs e)
{ Debug.WriteLine("PreviewMouseDownGrid"); }
void MouseDownGrid(object sender, RoutedEventArgs e)
{ Debug.WriteLine("MouseDownGrid"); }
void PreviewMouseDownCanvas(object sender, RoutedEventArgs e)
{ Debug.WriteLine("PreviewMouseDownCanvas"); }
void MouseDownCanvas(object sender, RoutedEventArgs e)
{ Debug.WriteLine("MouseDownCanvas"); }
void PreviewMouseDownEllipse(object sender, RoutedEventArgs e)
{ Debug.WriteLine("PreviewMouseDownEllipse"); }
void MouseDownEllipse(object sender, RoutedEventArgs e)
{ Debug.WriteLine("MouseDownEllipse"); }
}
}
Each handler prints out a debug message. Here is the debug output we get when clicking on theEllipseinside theCanvas:
PreviewMouseDownButton
PreviewMouseDownGrid
PreviewMouseDownCanvas
PreviewMouseDownEllipse
MouseDownEllipse
MouseDownCanvas
MouseDownGrid
This confirms that the preview event is raised first. It also shows that it starts from theButtonelement and works down, as we would expect with a tunneling event. The bubbling event that follows starts from theEllipseelement and works up. (Interestingly, it doesn’t appear to get as far as theButton. We’ll look at why this is shortly.)
This bubbling routing offered for most events means that you can register a single event handler on a control, and it will receive events for any of the elements nested inside the control. You do not need any special handling to deal with nested content, or controls whose appearance has been customized with templates—events simply bubble up to the control and can all be handled there.
Next: Halting Event Routing >>
More .NET Articles
More By O'Reilly Media
|
This article is excerpted from Programming WPF, Second Edition, written by Chris Sells and Ian Griffiths (O'Reilly, 2007; ISBN: 0596510373). Check it out today at your favorite bookstore. Buy this book now.
|
|