Udostępnij za pośrednictwem


How to: Create a Simple Event Handler

The steps to create an event handler in the Windows Presentation Foundation (WPF) Designer for Visual Studio are slightly different than the Windows Forms Designer. Starting in Visual Studio 2008 SP1, you can use the Events button in the Properties window. 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

  1. In XAML view, locate the element that you want to create an event handler for. This procedure will use the Button control.

  2. 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.

    IntelliSense list showing available events

  3. 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.Window1"
    xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" 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>
    
  4. 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.

    Navigate to Event Handler option

    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)

  1. In the Code Editor, open the Class Name drop-down list.

  2. Select the control or type that you want to create an event handler for.

  3. Open the Method Name drop-down list.

  4. 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

  1. In Design view, select the control that you want to create an event handler for.

  2. At the top of the Properties window, click the Events button.

    The events for the control are listed and the default event is selected.

    Note

    If you do not see the Events button, be sure you are using Visual Studio 2008 SP1 or later.

  3. Select an event and put the cursor in the value column.

  4. Type the event handler name or leave it blank to use the default name.

  5. 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.

See Also

Tasks

How to: Use Attached Events

Concepts

XAML Overview

Routed Events Overview

Code-Behind and XAML