For example, event routing
allows a click that begins in a toolbar button to rise up to the toolbar and then to the containing
window before it??™s handled by your code.
Event routing gives you the flexibility to write tight, well-organized code that handles
events in the most convenient place. It??™s also a necessity for working with the WPF content
model, which allows you to build simple elements (such as a button) out of dozens of distinct
ingredients, each of which has its own independent set of events.
Defining and Registering a Routed Event
The WPF event model is quite similar to the WPF property model. As with dependency
properties, routed events are represented by read-only shared fields, registered in a shared
constructor, and wrapped by a standard .NET event definition.
CHAPTER 6 n DEPENDENCY PROPERTIES AND ROUTED EVENTS 151
For example, the WPF Button class provides the familiar Click event, which is inherited
from the abstract ButtonBase class. Here??™s how the event is defined and registered:
Public MustInherit Class ButtonBase
Inherits ContentControl
Implements ...
' The event definition.
Public Shared ClickEvent As RoutedEvent
' The event registration.
Shared Sub New()
ButtonBase.ClickEvent = EventManager.RegisterRoutedEvent( _
"Click", RoutingStrategy.Bubble, GetType(RoutedEventHandler), _
GetType(ButtonBase))
.
Pages:
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308