Button Class

Definition

Represents a Windows button control, which reacts to the Click event.

public class Button : System.Windows.Controls.Primitives.ButtonBase
Inheritance
Derived

Inherited Members

System.Object

System.Windows.Controls.ContentControl

System.Windows.Controls.Control

System.Windows.Controls.Primitives.ButtonBase

System.Windows.DependencyObject

System.Windows.FrameworkElement

System.Windows.Media.Visual

System.Windows.Threading.DispatcherObject

System.Windows.UIElement

Examples

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>
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

Remarks

The Button class inherits directly from the System.Windows.Controls.Primitives.ButtonBase class.

Content Model: Button is a ContentControl. Its content property is Content.

Handle the System.Windows.Controls.Primitives.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.

Customizing the Button Control

To apply the same property settings to multiple Button controls, use the Style property. You can modify the default ControlTemplate to give the control a unique appearance. For more information about creating a ControlTemplate, see Customizing the Appearance of an Existing Control by Creating a ControlTemplate. To see the parts and states that are specific to the Button, see Button Styles and Templates.

Dependency properties for this control might be set by the control’s default style. If a property is set by a default style, the property might change from its default value when the control appears in the application. The default style is determined by which desktop theme is used when the application is running. For more information, see Default WPF Themes.

Note

Setting a visual property will only have an effect if that property is both present in Button control's default template and is set by using a . You can find a list of visual properties in the "Changing the Visual Structure of a Control" section in Customizing the Appearance of an Existing Control by Creating a ControlTemplate.

Constructors

Button()

Initializes a new instance of the Button class.

Fields

Is​Cancel​Property

Identifies the IsCancel dependency property.

Is​Defaulted​Property

Identifies the IsDefaulted dependency property.

Is​Default​Property

Identifies the IsDefault dependency property.

Properties

Is​Cancel

Gets or sets a value that indicates whether a Button is a Cancel button. A user can activate the Cancel button by pressing the ESC key.

Is​Default

Gets or sets a value that indicates whether a Button is the default button. A user invokes the default button by pressing the ENTER key.

Is​Defaulted

Gets a value that indicates whether a Button is the button that is activated when a user presses ENTER.

Methods

On​Click()

Called when a Button is clicked.

On​Create​Automation​Peer()

Creates an appropriate ButtonAutomationPeer for this control as part of the WPF infrastructure.