ButtonBase.Click 事件


在单击 Button 时发生。Occurs when a Button is clicked.

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




下面的示例演示三个按三种不同方式响应单击的按钮。The following example shows three buttons that respond to clicks in three different ways.

  • 悬停-当用户将鼠标悬停在按钮上时,第一个按钮更改颜色Hover - the first button changes colors when the user hovers with the mouse over the button

  • 按下-第二个按钮要求在鼠标指针位于按钮上时按下鼠标。Press - the second button requires that the mouse be pressed while the mouse pointer is in the button.

  • Release-第三方不会重置按钮的背景色,直到在按钮中按下并释放鼠标。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">

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

<Button Name="btn3" 
        Click="OnClick3" ClickMode="Release">
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


用户可以 Click 通过使用 AccessText 或在控件具有焦点时按 enter 键或空格键来引发事件。The user can raise the Click event by using an AccessText or by pressing ENTER or the SPACEBAR when the control has focus. 当用户按空格键时,控件将设置 IsPressedtrue 并捕获鼠标。When the user presses the SPACEBAR, the control sets IsPressed to true and captures the mouse. 因此,控件将引发鼠标事件,例如 MouseEnterIsMouseDirectlyOverChangedAs a result, the control will raise mouse events such as MouseEnter and IsMouseDirectlyOverChanged. 请注意,使用 AccessText 或 ENTER 不会更改 IsPressed 或捕获鼠标,但会引发 Click 事件。Note that using the AccessText or ENTER does not change IsPressed or capture the mouse, but it does raise the Click event.

ButtonBase MouseLeftButtonDown 方法中将事件标记为已处理 OnMouseLeftButtonDown 并引发 Click 事件。The ButtonBase marks the MouseLeftButtonDown event as handled in the OnMouseLeftButtonDown method and raises the Click event. 因此, OnMouseLeftButtonDown 对于从继承的控件,该事件永远不会发生 ButtonBaseHence, the OnMouseLeftButtonDown event will never occur for a control that inherits from ButtonBase. 改为将事件处理程序附加到 PreviewMouseLeftButtonDown 事件,或将 AddHandler(RoutedEvent, Delegate, Boolean) handledEventsToo 设置为时调用 trueInstead, attach an event handler to the PreviewMouseLeftButtonDown event, or call AddHandler(RoutedEvent, Delegate, Boolean) with handledEventsToo set to true.

路由事件信息Routed Event Information

标识符字段Identifier field ClickEvent
路由策略Routing strategy 冒泡Bubbling
委托Delegate RoutedEventHandler