Represents a button control.
Namespace:
System.Windows.Controls
Assembly:
System.Windows (in System.Windows.dll)
Visual Basic (Declaration)
<TemplateVisualStateAttribute(Name := "Focused", GroupName := "FocusStates")> _
<TemplateVisualStateAttribute(Name := "Normal", GroupName := "CommonStates")> _
<TemplateVisualStateAttribute(Name := "MouseOver", GroupName := "CommonStates")> _
<TemplateVisualStateAttribute(Name := "Pressed", GroupName := "CommonStates")> _
<TemplateVisualStateAttribute(Name := "Disabled", GroupName := "CommonStates")> _
<TemplateVisualStateAttribute(Name := "Unfocused", GroupName := "FocusStates")> _
Public Class Button _
Inherits ButtonBase
[TemplateVisualStateAttribute(Name = "Focused", GroupName = "FocusStates")]
[TemplateVisualStateAttribute(Name = "Normal", GroupName = "CommonStates")]
[TemplateVisualStateAttribute(Name = "MouseOver", GroupName = "CommonStates")]
[TemplateVisualStateAttribute(Name = "Pressed", GroupName = "CommonStates")]
[TemplateVisualStateAttribute(Name = "Disabled", GroupName = "CommonStates")]
[TemplateVisualStateAttribute(Name = "Unfocused", GroupName = "FocusStates")]
public class Button : ButtonBase
XAML Object Element Usage
<Button .../>
-or-
<button>
singleObject
</button>
XAML Values
- button
A Button object element, or an object element for a class that derives from Button.
- singleObject
A single object element that declares the content.
The following illustration shows Button controls in default and disabled states.
Button Controls
.png)
When you click a Button, it raises the Click event. By default, if a button has the focus, pressing the ENTER key or the SPACEBAR also raises the Click event.
You can change how a button raises the Click event by changing the ClickMode property. The default ClickMode value is Release. If the ClickMode is set to Hover, the Click event cannot be raised with the keyboard.
Button is a ContentControl. Its content property is Content. For more information about content models, see Control Content Models.
Customizing the Button Control
To apply the same property settings to multiple Button controls, use the Style property. To change the visual structure and visual behavior of a Button, copy and modify its default style and template. For more information, see Control Customization.
If a dependency property for a Button is set by its default style, the property might change from its default value when the Button appears in the application. For more information, see Dependency Property Value Precedence. You can get the default style and template for Button from Button Styles and Templates.
The following example shows three buttons that respond to clicks in three different ways based on their ClickMode property value.
Hover - When the mouse pointer hovers over the first button, the foreground color of the button changes.
Press - When the left mouse button is pressed over the second button, the foreground color of the button changes.
Release - When the mouse button is pressed and released when over the third button, the button resets the foreground color of the other two buttons to their original color.
Run this sample
Private Sub OnClick1(ByVal sender As Object, ByVal e As RoutedEventArgs)
btn1.Foreground = New SolidColorBrush(Colors.Blue)
text1.Text = "Click event handled on Hover."
text2.Text = ""
text3.Text = ""
End Sub
Private Sub OnClick2(ByVal sender As Object, ByVal e As RoutedEventArgs)
btn2.Foreground = New SolidColorBrush(Colors.Green)
text1.Text = ""
text2.Text = "Click event handled on Press."
text3.Text = ""
End Sub
Private Sub OnClick3(ByVal sender As Object, ByVal e As RoutedEventArgs)
btn1.Foreground = New SolidColorBrush(Colors.Green)
btn2.Foreground = New SolidColorBrush(Colors.Blue)
text1.Text = ""
text2.Text = ""
text3.Text = "Click event handled on Release."
End Sub
void OnClick1(object sender, RoutedEventArgs e)
{
btn1.Foreground = new SolidColorBrush(Colors.Blue);
text1.Text = "Click event handled on Hover.";
text2.Text = "";
text3.Text = "";
}
void OnClick2(object sender, RoutedEventArgs e)
{
btn2.Foreground = new SolidColorBrush(Colors.Green);
text1.Text = "";
text2.Text = "Click event handled on Press.";
text3.Text = "";
}
void OnClick3(object sender, RoutedEventArgs e)
{
btn1.Foreground = new SolidColorBrush(Colors.Green);
btn2.Foreground = new SolidColorBrush(Colors.Blue);
text1.Text = "";
text2.Text = "";
text3.Text = "Click event handled on Release.";
}
<StackPanel x:Name="LayoutRoot" Background="White" Margin="10">
<Button x:Name="btn1" Margin ="5"
HorizontalAlignment="Left"
Foreground="Green" Width="120" Click="OnClick1"
Content="Hover to Click" ClickMode="Hover" />
<TextBlock x:Name="text1" Margin ="0,8,0,0" />
<Button x:Name="btn2" Margin ="5,5,5,5"
HorizontalAlignment="Left"
Foreground="Blue" Width="120" Click="OnClick2"
Content="Press to Click" ClickMode="Press" />
<TextBlock x:Name="text2" Margin="0,8,0,0" />
<Button x:Name="btn3" Margin ="5,5,5,5"
HorizontalAlignment="Left"
Click="OnClick3" Width="120" Content="Reset"
ClickMode="Release"/>
<TextBlock x:Name="text3" Margin ="0,8,0,0" />
</StackPanel>
System..::.Object
System.Windows..::.DependencyObject
System.Windows..::.UIElement
System.Windows..::.FrameworkElement
System.Windows.Controls..::.Control
System.Windows.Controls..::.ContentControl
System.Windows.Controls.Primitives..::.ButtonBase
System.Windows.Controls..::.Button
System.Windows.Controls.Primitives..::.CalendarButton
System.Windows.Controls.Primitives..::.CalendarDayButton
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.
Reference
Other Resources