Prev | Current Page 313 | Next

Matthew MacDonald

"Pro WPF with VB 2008: Windows Presentation Foundation with .NET 3.5"

Instead of attaching
the event handler through XAML, you must use the AddHandler() method described earlier.
The AddHandler() method provides an overload that accepts a Boolean value for its third
parameter. Set this to True, and you??™ll receive the event even if the Handled flag has been set:
cmdClear.AddHander(UIElement.MouseUpEvent, _
New MouseButtonEventHandler(cmdClear_MouseUp), True)
This is rarely a good design decision. The button is designed to suppress the MouseUp
event for a reason: to prevent possible confusion. After all, it??™s a common Windows convention
that buttons can be ???clicked??? with the keyboard in several ways. If you make the mistake of
handling the MouseUp event in a Button instead of the Click event, your code will only
respond to mouse clicks, not the equivalent keyboard actions.
Attached Events
The fancy label example is a fairly straightforward example of event bubbling because all the
elements support the MouseUp event. However, many controls have their own more specialized
events. The button is one example??”it adds a Click event that isn??™t defined by any base
class.
This introduces an interesting dilemma. Imagine you wrap a stack of buttons in a Stack-
Panel. You want to handle all the button clicks in one event handler. The crude approach is to
attach the Click event of each button to the same event handler.


Pages:
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325