Prev | Current Page 304 | Next

Matthew MacDonald

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


If you want to detach an event handler, code is your only option. You can use the Remove-
Handler statement , as shown here:
RemoveHandler img.MouseUp, AddressOf img_MouseUp
Or you can use the UIElement.RemoveHandler() method:
img.RemoveHandler(Image.MouseUpEvent, _
New MouseButtonEventHandler(AddressOf img_MouseUp))
CHAPTER 6 n DEPENDENCY PROPERTIES AND ROUTED EVENTS 155
It is technically possible to connect the same event handler to the same event more than
once. This is usually the result of a coding mistake. (In this case, the event handler will be triggered
multiple times.) If you attempt to remove an event handler that??™s been connected twice,
the event will still trigger the event handler, but just once.
Event Routing
As you learned in the previous chapter, many controls in WPF are content controls, and content
controls can hold any type and amount of nested content. For example, you can build a
graphical button out of shapes, create a label that mixes text and pictures, or put content in a
specialized container to get a scrollable or collapsible display. You can even repeat this nesting
process to go as many layers deep as you want.
This ability for arbitrary nesting raises an interesting question. For example, imagine you
have a label like this one, which contains a StackPanel that brings together two blocks of text
and an image: