Compartilhar via


Como: Criar um simples manipulador de eventos

As etapas para criar um manipulador de eventos no Windows Presentation Foundation (WPF) Designer for Visual Studio são ligeiramente diferentes de Designer de Formulários do Windows. A partir de Visual Studio 2008 SP1, você pode usar no botão eventos na janela Propriedades. 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.

Para criar um manipulador de eventos 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, aparece uma lista do IntelliSense com sistema autônomo eventos disponível, sistema autônomo mostrado na ilustração a seguir.

  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çã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.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. No modo de exibição XAML, clicar com o botão direito do mouse no evento ou o nome do manipulador de eventos e selecionar o Navegar até manipulador de eventos opção, conforme mostrado na ilustração a seguir.

    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 eventos usando o posicionamento do Editor de códigos - down listas (Visual Basic somente)

  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 eventos usando o botão de eventos

  1. No modo de modo de exibição de Design, selecionar o controle que você deseja criar um manipulador de eventos para.

  2. Na parte superior do Propriedades janela, clicar no botão Events.

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

    Observação:

    Se não for exibida no botão Events, certifique-se de que você está usando Visual Studio 2008 SP1 ou posterior.

  3. selecionar um evento e coloque o cursor na coluna de valor.

  4. Tipo de manipulador de eventos name ou deixe-o em branco para usar o nome padrão.

  5. Para criar o manipulador de eventos, pressione ENTER ou clicar duas vezes na coluna de valor.

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

Consulte também

Tarefas

Como: Use eventos anexados

Conceitos

XAML Overview

Visão geral sobre eventos roteados

Code-Behind e XAML