How to: Create a Simple Event Handler
The steps to create an event handler in the WPF Designer for Visual Studio are slightly different than the Windows Forms Designer. The following procedures describe ways to create simple event handlers in the WPF Designer.
To create a default event handler
In Design view, double-click the control that you want to create an event handler for.
The default event handler for the control is created. The code-behind file is opened and the cursor is positioned in the default event handler.
To create an event handler in XAML
In XAML view, locate the element that you want to create an event handler for. This procedure will use the Button control.
When you begin typing the event name, an IntelliSense list appears with the available events, as shown in the following illustration.
In the attribute value, type the name of the event handler. In the IntelliSense list that appears, you can double-click <New Event Handler> to use the default event handler name.
If you just select <New Event Handler> (without double-clicking), a tooltip appears providing additional information about the method that will be created.
<Window x:Class="WPFApplication.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="300" Width="300"> <Grid> <Button Height="23" HorizontalAlignment="Left" Margin="33,38,0,0" Name="Button1" VerticalAlignment="Top" Width="75" Click="button1_Click" MouseEnter="button1_MouseEnter"> Button</Button> </Grid> </Window>
In XAML view, right-click the event or event handler name and select the Navigate to Event Handler option, as shown in the following illustration.
The code-behind file is opened and the cursor is positioned in the selected event handler. The following code shows example event handlers.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) End Sub Private Sub Button1_MouseEnter(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseEventArgs) End Sub
To create an event handler by using the Code Editor drop-down lists (Visual Basic only)
In the Code Editor, open the Class Name drop-down list.
Select the control or type that you want to create an event handler for.
Open the Method Name drop-down list.
Select the event that you want create an event handler for.
An event handler is created and the cursor is positioned in the event handler.
To create an event handler by using the Events button
In Design view, select the control that you want to create an event handler for.
At the top of the Properties window, click the Events button.
The events for the control are listed and the default event is selected.
Select an event and put the cursor in the value column.
Type the event handler name or leave it blank to use the default name.
To create the event handler, press ENTER or double-click the value column.
The event handler for the control is created. The code-behind file is opened and the cursor is positioned in the event handler. For Visual C# projects, an attribute that specifies the event handler is added to the XAML file. For Visual Basic projects, the XAML file is not modified.