Share via


How to: Create a Simple Event Handler

As etapas para criar um manipulador de evento na WPF Designer for Visual Studio são ligeiramente diferentes do Designer de Windows Forms . Os procedimentos a seguir descrevem maneiras de criar manipuladores de evento de simples na 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.

Para criar um manipulador de evento em 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.

    Quando você começar a digitar o nome do evento , será exibida uma lista de IntelliSense com os eventos disponíveis, conforme mostrado na ilustração a seguir.

    Lista do IntelliSense mostrando eventos disponíveis

  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.

    ObservaçãoObservação

    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>
    
  4. No modo de exibição XAML , direito, clique no evento ou o nome do manipulador de evento de-e selecione o navegar até manipulador de eventos opção, conforme mostrado na ilustração a seguir.

    Opção Navegar até Manipulador de Eventos

    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)
    {
    
    }
    

Para criar um manipulador de evento usando o Editor de Código-listas suspensas (somente para o Visual Basic)

  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.

Para criar um manipulador de evento usando o botão de eventos

  1. No modo de exibição de Design, selecione o controle que você deseja criar um manipulador de evento .

  2. Na parte superior a Propriedades janela, clique no eventos botão.

    Os eventos do controle são listados e o padrão de evento está selecionada.

  3. Selecione um evento e colocar o cursor na coluna value.

  4. OTipo nome de manipulador de evento ou deixe-a em branco para usar o nome padrão.

  5. Para criar o manipulador de evento , pressione ENTER ou clique de-duplo a coluna de valor.

    O manipulador de evento para o controle é criado. O arquivo code-behind é aberto e o cursor está posicionado no manipulador de evento . Para Visual C# projetos, um atributo que especifica o manipulador de evento é adicionado ao arquivo XAML . Para Visual Basic projetos, o arquivo XAML não é modificada.

Consulte também

Tarefas

Como: usar anexadas eventos

Conceitos

Visão geral do XAML (WPF)

Visão geral sobre eventos roteados

Code-Behind e XAML no WPF