Prev | Current Page 139 | Next

Matthew MacDonald

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

If you need to create a new event handler to handle this event,
you simply need to choose from the top of the list.
CHAPTER 2 n XAML 44
Figure 2-2. Attaching an event with Visual Studio IntelliSense
There??™s one wrinkle in the way in the way WPF event handlers work with VB. As you no
doubt know, Visual Basic defines a Handles statement that allows you to hook up events
declaratively in code. WPF supports this convention.
To use the Handles statement, your element must have a name. Provided this requirement
is met, you can use the Handles statement in the familiar form, supplying
ElementName.EventName, as shown here:
Private Sub cmdAnswer_Click(ByVal sender As Object, _
ByVal e As RoutedEventArgs e) Handles cmdAnswer.Click
...
End Sub
In this case, no event attribute is required in the XAML. (In fact, you must be careful not to
supply an event attribute, or you??™ll end up connecting the same event handler twice.) If you
double-click an element on the Visual Studio design surface, Visual Studio creates this type of
event handler automatically. It even assigns an automatically generated name to the element
if it doesn??™t already have a name to ensure that this system works.
At first glance, this seems like a perfect solution, because it preserves the convenient VB
event handling approach with minimum fuss.


Pages:
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151