Prev | Current Page 298 | Next

Matthew MacDonald

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


CHAPTER 6 n DEPENDENCY PROPERTIES AND ROUTED EVENTS 152
nNote It??™s easy to confuse the AddHandler() and RemoveHandler() methods of the FrameworkElement
class with the AddHandler and RemoveHandler statements in the Visual Basic language. Although they have
similar names, they work a bit differently. The FrameworkElement methods offer the most direct approach??”
they plug directly into the routed event model. The VB statements use the event wrapper, which then calls
the FrameworkElement methods. Both approaches have the same end result.
Of course, like any event, the defining class needs to raise it at some point. Exactly where
this takes place is an implementation detail. However, the important detail is that your event
is not raised through the traditional .NET event wrapper. Instead, you use the RaiseEvent()
method that every element inherits from the UIElement class. Here??™s the appropriate code
from deep inside the ButtonBase class:
Dim e As New RoutedEventArgs(ButtonBase.ClickEvent, Me)
MyBase.RaiseEvent(e)
The RaiseEvent() method takes care of firing the event to every caller that??™s been registered
with the AddHandler() method. Because AddHandler() is public, callers have a
choice??”they can register themselves directly by calling AddHandler(), or they can use the
event wrapper.


Pages:
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310