Prev | Current Page 303 | Next

Matthew MacDonald

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

Add-
Handler() method, as shown in the previous section. You can also connect an event directly by
calling UIElement.AddHandler() method yourself. Here??™s an example:
img.AddHandler(Image.MouseUpEvent, _
New MouseButtonEventHandler(AddressOf img_MouseUp))
When you use this approach, you always need to create the appropriate delegate type
(such as MouseButtonEventHandler). You can??™t create the delegate object implicitly, as
you can when hooking up an event through the property wrapper. That??™s because the
UIElement.AddHandler() method supports all WPF events and it doesn??™t know the delegate
type that you want to use.
Some developers prefer to use the name of the class where the event is defined, rather
than the name of the class that is firing the event. Here??™s the equivalent syntax that makes it
clear that the MouseUpEvent is defined in UIElement:
img.AddHandler(UIElement.MouseUpEvent, _
New MouseButtonEventHandler(AddressOf img_MouseUp))
nNote Which approach you use is largely a matter of taste. However, the drawback to this second approach
is that it doesn??™t make it obvious that the Image class provides a MouseUpEvent. It??™s possible to confuse this
code and assume it??™s attaching an event handler that??™s meant to deal with the MouseUpEvent in a nested element.
You??™ll learn more about this technique in the section ???Attached Events??? later in this chapter.


Pages:
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315