ButtonBase.Click Evento

Definição

Ocorre quando um Button é clicado.Occurs when a Button is clicked.

public:
 event System::Windows::RoutedEventHandler ^ Click;
public event System.Windows.RoutedEventHandler Click;
member this.Click : System.Windows.RoutedEventHandler 
Public Custom Event Click As RoutedEventHandler 

Tipo de evento

RoutedEventHandler

Exemplos

O exemplo a seguir mostra três botões que respondem a cliques de três maneiras diferentes.The following example shows three buttons that respond to clicks in three different ways.

  • Focalizar – o primeiro botão altera as cores quando o usuário passa o mouse sobre o botãoHover - the first button changes colors when the user hovers with the mouse over the button

  • Pressionar-o segundo botão requer que o mouse seja pressionado enquanto o ponteiro do mouse estiver no botão.Press - the second button requires that the mouse be pressed while the mouse pointer is in the button.

  • Versão – o terceiro não redefine a cor do plano de fundo dos botões até que o mouse seja pressionado e liberado no botão.Release - the third does not reset the background color of the buttons until the mouse is pressed and released in the button.

<Button Name="btn1" Background="Pink" 
        BorderBrush="Black" BorderThickness="1" 
        Click="OnClick1" ClickMode="Hover">
  ClickMe1
</Button>

<Button Name="btn2" Background="LightBlue" 
        BorderBrush="Black" BorderThickness="1" 
        Click="OnClick2" ClickMode="Press">
  ClickMe2
</Button>

<Button Name="btn3" 
        Click="OnClick3" ClickMode="Release">
  Reset
</Button>
void OnClick1(object sender, RoutedEventArgs e)
{
    btn1.Background = Brushes.LightBlue;
}

void OnClick2(object sender, RoutedEventArgs e)
{
    btn2.Background = Brushes.Pink;
}

void OnClick3(object sender, RoutedEventArgs e)
{
    btn1.Background = Brushes.Pink;
    btn2.Background = Brushes.LightBlue;
}
Private Sub OnClick1(ByVal sender As Object, ByVal e As RoutedEventArgs)
    btn1.Background = Brushes.LightBlue
End Sub

Private Sub OnClick2(ByVal sender As Object, ByVal e As RoutedEventArgs)
    btn2.Background = Brushes.Pink
End Sub

Private Sub OnClick3(ByVal sender As Object, ByVal e As RoutedEventArgs)
    btn1.Background = Brushes.Pink
    btn2.Background = Brushes.LightBlue
End Sub

Comentários

O usuário pode gerar o Click evento usando um AccessText ou pressionando ENTER ou a barra de espaços quando o controle tiver foco.The user can raise the Click event by using an AccessText or by pressing ENTER or the SPACEBAR when the control has focus. Quando o usuário pressiona a barra de espaços, o controle é definido IsPressed para true e captura o mouse.When the user presses the SPACEBAR, the control sets IsPressed to true and captures the mouse. Como resultado, o controle irá gerar eventos do mouse, como MouseEnter e IsMouseDirectlyOverChanged .As a result, the control will raise mouse events such as MouseEnter and IsMouseDirectlyOverChanged. Observe que o uso de AccessText ou Enter não altera IsPressed nem captura o mouse, mas gera o Click evento.Note that using the AccessText or ENTER does not change IsPressed or capture the mouse, but it does raise the Click event.

O ButtonBase marca o MouseLeftButtonDown evento como manipulado no OnMouseLeftButtonDown método e gera o Click evento.The ButtonBase marks the MouseLeftButtonDown event as handled in the OnMouseLeftButtonDown method and raises the Click event. Portanto, o OnMouseLeftButtonDown evento nunca ocorrerá para um controle herdado de ButtonBase .Hence, the OnMouseLeftButtonDown event will never occur for a control that inherits from ButtonBase. Em vez disso, anexe um manipulador de eventos ao PreviewMouseLeftButtonDown evento ou chame AddHandler(RoutedEvent, Delegate, Boolean) with handledEventsToo definido como true .Instead, attach an event handler to the PreviewMouseLeftButtonDown event, or call AddHandler(RoutedEvent, Delegate, Boolean) with handledEventsToo set to true.

Informações de evento encaminhadoRouted Event Information

Campo de identificadorIdentifier field ClickEvent
Estratégia de roteamentoRouting strategy PropagaçãoBubbling
DelegarDelegate RoutedEventHandler

Aplica-se a