Events and Event Handlers
There are two ways to declare event handlers in Silverlight 2. The first is directly in the XAML:
<Button x:Name="myPushyButton" Content="Click Me Please" Height="30" Width="100" Grid.Column="1" Grid.Row="4" Click="myPushyButton_Click" />
Figure 7-12. The completed Grid
When you declare a button in the XAML, IntelliSense is available to help you create the event handler name (as shown in Figure 7-13).
Figure 7-13. Inline event handler
If you use IntelliSense to wire the event handler, a skeleton event handler method is created in the code-behind (Page.xaml.cs):
private void myPushyButton_Click(object sender, Routed EventArgs e { myPushyButton.Width *= 1.25; myPushyButton.Content = "Thanks, I needed that!"; }
The first thing to notice is that the name of the method is identical to that declared in the XAML.
The second is that this method follows the pattern of all .NET event handlers: it returns void
and takes two parameters. The first is of type object
and contains a reference to the object that raised the event. The second is of type EventArgs
, or a type that derives from EventArgs
(in this case, RoutedEventArgs
). Also notice that nowhere in the code-behind do you see anything like this:
Button myPushyButton = GetTheButtonIDeclaredinTheXAML
Any object declared in the ...
Get Programming .NET 3.5 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.