ButtonBase.Click 事件

定义

在单击 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 

示例

下面的示例演示三个按三种不同方式响应单击的按钮。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">
  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

注解

用户可以通过AccessText使用或Click在控件具有焦点时按 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. 因此, 控件将引发鼠标事件, 例如MouseEnter和。 IsMouseDirectlyOverChangedAs 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 方法OnMouseLeftButtonDownMouseLeftButtonDown将事件标记为已处理并引发Click事件。The ButtonBase marks the MouseLeftButtonDown event as handled in the OnMouseLeftButtonDown method and raises the Click event. 因此, 对于OnMouseLeftButtonDownButtonBase继承的控件, 该事件永远不会发生。Hence, the OnMouseLeftButtonDown event will never occur for a control that inherits from ButtonBase. 改为将事件处理程序附加到PreviewMouseLeftButtonDown事件, 或将AddHandler(RoutedEvent, Delegate, Boolean)设置handledEventsTootrue时调用。Instead, 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

适用于