Updated: November 2007
Represents a Windows button control, which reacts to the ButtonBase..::.Click event.
Public Class Button _ Inherits ButtonBase
Dim instance As Button
public class Button : ButtonBase
public ref class Button : public ButtonBase
public class Button extends ButtonBase
<Button> Content </Button>
The Button class inherits directly from the System.Windows.Controls.Primitives..::.ButtonBase class.
Content Model: Button is a ContentControl. Its content property is Content. For more information on the content model for Button, see Controls Content Model Overview.
Handle the ButtonBase..::.Click event to respond when the user clicks a Button.
The OnMouseLeftButtonDown method marks the MouseLeftButtonDown event as handled. To respond to the MouseLeftButtonDown event, attach an event handler to the PreviewMouseLeftButtonDown event, or call AddHandler(RoutedEvent, Delegate, Boolean) with handledEventsToo set to true.
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 over the button.
Release: the third does not reset the background color of the buttons until the mouse is pressed and released on 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>
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
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; }
Windows Vista