ButtonBase.Click ButtonBase.Click ButtonBase.Click ButtonBase.Click Event

Definición

Se genera cuando se hace clic en Button.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 

Ejemplos

El ejemplo siguiente muestra tres botones que responden a clics de tres maneras diferentes.The following example shows three buttons that respond to clicks in three different ways.

  • Al mantener el mouse - el primer botón cambia de color cuando el usuario desplace el mouse sobre el botónHover - the first button changes colors when the user hovers with the mouse over the button

  • Presionar: el segundo botón requiere que se presione el mouse mientras el puntero del mouse se encuentra en el botón.Press - the second button requires that the mouse be pressed while the mouse pointer is in the button.

  • Versión: el tercero no restablece el color de fondo de los botones hasta que se presiona y suelta en el botón del mouse.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

Comentarios

El usuario puede provocar la Click eventos mediante el uso de un AccessText o presionando ENTRAR o la barra espaciadora cuando el control tiene el foco.The user can raise the Click event by using an AccessText or by pressing ENTER or the SPACEBAR when the control has focus. Cuando el usuario presiona la barra espaciadora, los conjuntos de controles IsPressed a true y captura el mouse.When the user presses the SPACEBAR, the control sets IsPressed to true and captures the mouse. Como resultado, el control generará eventos del mouse como MouseEnter y IsMouseDirectlyOverChanged.As a result, the control will raise mouse events such as MouseEnter and IsMouseDirectlyOverChanged. Tenga en cuenta que el uso del AccessText o ENTRAR no cambia IsPressed o capturar el mouse, pero provoca la Click eventos.Note that using the AccessText or ENTER does not change IsPressed or capture the mouse, but it does raise the Click event.

El ButtonBase marcas el MouseLeftButtonDown evento como controlado en el OnMouseLeftButtonDown método y genera el Click eventos.The ButtonBase marks the MouseLeftButtonDown event as handled in the OnMouseLeftButtonDown method and raises the Click event. Por lo tanto, el OnMouseLeftButtonDown nunca se producirá el evento para un control que hereda de ButtonBase.Hence, the OnMouseLeftButtonDown event will never occur for a control that inherits from ButtonBase. En su lugar, adjuntar un controlador de eventos para el PreviewMouseLeftButtonDown llamada o evento AddHandler(RoutedEvent, Delegate, Boolean) con handledEventsToo establecido en true.Instead, attach an event handler to the PreviewMouseLeftButtonDown event, or call AddHandler(RoutedEvent, Delegate, Boolean) with handledEventsToo set to true.

Información sobre eventos enrutadosRouted Event Information

Campo de identificadorIdentifier field ClickEvent
Estrategia de enrutamientoRouting strategy PropagaciónBubbling
delegadoDelegate RoutedEventHandler

Se aplica a