Printer Friendly Version      Send     
Click to Rate and Give Feedback
MSDN
MSDN Library
Web Development
Silverlight
Button Class
Other versions are also available for the following:
.NET Framework Class Library for Silverlight
Button Class
[This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]

Represents a button control, which responds to the ButtonBase..::.Click event.

Namespace:  System.Windows.Controls
Assembly:  System.Windows (in System.Windows.dll)
XMLNS for XAML: Not mapped to an xmlns.

Visual Basic (Declaration)
<TemplateVisualStateAttribute(Name := "Unfocused", GroupName := "FocusStates")> _
<TemplateVisualStateAttribute(Name := "MouseOver", GroupName := "CommonStates")> _
<TemplateVisualStateAttribute(Name := "Pressed", GroupName := "CommonStates")> _
<TemplateVisualStateAttribute(Name := "Focused", GroupName := "FocusStates")> _
<TemplateVisualStateAttribute(Name := "Disabled", GroupName := "CommonStates")> _
<TemplateVisualStateAttribute(Name := "Normal", GroupName := "CommonStates")> _
Public Class Button _
    Inherits ButtonBase
Visual Basic (Usage)
Dim instance As Button
C#
[TemplateVisualStateAttribute(Name = "Unfocused", GroupName = "FocusStates")]
[TemplateVisualStateAttribute(Name = "MouseOver", GroupName = "CommonStates")]
[TemplateVisualStateAttribute(Name = "Pressed", GroupName = "CommonStates")]
[TemplateVisualStateAttribute(Name = "Focused", GroupName = "FocusStates")]
[TemplateVisualStateAttribute(Name = "Disabled", GroupName = "CommonStates")]
[TemplateVisualStateAttribute(Name = "Normal", GroupName = "CommonStates")]
public class Button : ButtonBase
Visual C++
[TemplateVisualStateAttribute(Name = L"Unfocused", GroupName = L"FocusStates")]
[TemplateVisualStateAttribute(Name = L"MouseOver", GroupName = L"CommonStates")]
[TemplateVisualStateAttribute(Name = L"Pressed", GroupName = L"CommonStates")]
[TemplateVisualStateAttribute(Name = L"Focused", GroupName = L"FocusStates")]
[TemplateVisualStateAttribute(Name = L"Disabled", GroupName = L"CommonStates")]
[TemplateVisualStateAttribute(Name = L"Normal", GroupName = L"CommonStates")]
public ref class Button : public ButtonBase
JScript
public class Button extends ButtonBase
XAML Object Element Usage
<Button .../>

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

Handle the ButtonBase..::.Click event to respond when the user clicks a Button. You can change how the button raises the ButtonBase..::.Click event by changing the ClickMode property. The default ClickMode value is Release.

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

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 while over the second button, the foreground color of the button changes.

  • Release - When the mouse button is pressed and released while over the third button, the button resets the foreground color of the other two buttons to their original color.

XAML
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>

        <TextBlock Text="Button Demonstration" Margin="0,20,10,15"
            FontFamily="Verdana" FontSize="18" FontWeight="Bold"
            Foreground="#FF5C9AC9" Grid.Row="0" Grid.ColumnSpan="2"/>

        <Button x:Name="btn1" Grid.Row="1" Margin ="0,5,5,5" 
            HorizontalAlignment="Left"
            Foreground="Green" Width="120" Click="OnClick1" 
            Content="Hover to Click" ClickMode="Hover" />
        <TextBlock x:Name="text1" Grid.Row="1" Grid.Column="1" 
            Margin ="0,8,0,0" />

        <Button x:Name="btn2" Grid.Row="2" Margin ="0,5,5,5" 
            HorizontalAlignment="Left" 
            Foreground="Blue" Width="120" Click="OnClick2" 
            Content="Press to Click" ClickMode="Press" />
        <TextBlock x:Name="text2" Grid.Row="2" Grid.Column="1" 
            Margin="0,8,0,0" />

        <Button x:Name="btn3" Grid.Row="3" Margin ="0,5,5,5" 
            HorizontalAlignment="Left"
            Click="OnClick3" Width="120" Content="Reset" 
            ClickMode="Release"/>
        <TextBlock x:Name="text3" Grid.Row="3" Grid.Column="1" 
            Margin ="0,8,0,0" />

C#
void OnClick1(object sender, RoutedEventArgs e)
{
    btn1.Foreground = new SolidColorBrush(Colors.Blue);
    text1.Text = "Click event handled on Hover.";
    text3.Text = "";
}

void OnClick2(object sender, RoutedEventArgs e)
{
    btn2.Foreground = new SolidColorBrush(Colors.Green);
    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.";
}

Run this sample

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker