How to: Create a Simple Event Handler
[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]
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.
In the start tag for the element, begin typing the event name that you want to handle, for example the Click or the MouseEnter event.
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.
Note
If you just select <New Event Handler> (without double-clicking), a tooltip appears providing additional information about the method that will be created.
The following XAML specifies a Click event handler and a MouseEnter event handler for a Button control.
<Window x:Class="WPFApplication.MainWindow" xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="https://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
private void button1_Click(object sender, RoutedEventArgs e) { } private void button1_MouseEnter(object sender, MouseEventArgs e) { }
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.