Mouse Input and the WPF
(Page 1 of 4 )
This second part of a five-part article series on the input handling mechanisms available with the Windows Presentation Foundation focuses on the mouse. It is excerpted from
Programming WPF, Second Edition, written by Chris Sells and Ian Griffiths (O'Reilly, 2007; ISBN: 0596510373). Copyright © 2007 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.
Attached Events
It is possible to define an attached event. This is the routed-event equivalent of an attached property: an event defined by a different class than the one from which the event will be raised. This keeps the input system open to extension. If a new kind of input device is invented, it could define new events as attached events, enabling them to be raised from any UI element.
In fact, the WPF input system already works this way. The mouse, stylus, and keyboard events examined in this chapter are just wrappers for underlying attached events defined by theMouse,Keyboard, andStylus classes in theSystem.Windows.Inputnamespace. This means we could change theGrid element in Example 4-1 to use the attached events defined by theMouseclass, as shown in Example 4-6.
Example 4-6. Attached event handling
<Grid Mouse.PreviewMouseDown="PreviewMouseDownGrid"
Mouse.MouseDown="MouseDownGrid">
This would have no effect on the behavior, because the names Example 4-1 used for these events are aliases for the attached events used in this example.
Handling attached events from code looks a little different. Normal CLR events don’t support this notion of attached events, so we can’t use the ordinary C# event syntax like we did in Example 4-4. Instead, we have to call the AddHandlermethod, passing in theRoutedEventobject representing the attached event (see Example 4-7).
Example 4-7. Explicit attached event handling
myEllipse.AddHandler(Mouse.PreviewMouseDownEvent,
new MouseButtonEventHandler(PreviewMouseDownEllipse)); myEllipse.AddHandler(Mouse.MouseDownEvent,
new MouseButtonEventHandler(MouseDownEllipse));
Alternatively, we can use the helper functions provided by theMouseclass. Example 4-8 uses this to perform exactly the same job as the preceding two examples.
Example 4-8. Attached event handling with helper function
Mouse.AddPreviewMouseDownHandler(myEllipse, PreviewMouseDownEllipse); Mouse.AddMouseDownHandler(myEllipse, MouseDownEllipse);
Example 4-8 is more compact than Example 4-7 because we were able to omit the explicit construction of the delegate, relying instead on C# delegate type inference. Example 4-7 cannot do this because AddHandlercan attach a handler for any kind of event, so in its function signature the second parameter is of the baseDelegate type. By convention, classes that define attached events usually provide corresponding helper methods like these to let you use this slightly neater style of code.
Next: Mouse Input >>
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.
|
|