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 - 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. 因此, 控制項將會引發滑鼠事件, 例如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.

會在OnMouseLeftButtonDown方法MouseLeftButtonDown中將事件Click標示為已處理, 並引發事件。 ButtonBaseThe 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附加至事件, 或handledEventsToo使用設定true為的呼叫AddHandler(RoutedEvent, Delegate, Boolean)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 - 委派Delegate RoutedEventHandler