Skip to content

Latest commit

 

History

History
91 lines (54 loc) · 6.1 KB

button.md

File metadata and controls

91 lines (54 loc) · 6.1 KB
-api-id -api-type
T:Windows.UI.Xaml.Controls.Button
winrt class

Windows.UI.Xaml.Controls.Button

-description

Represents a templated button control that interprets a Click user interaction.

-xaml-syntax

<Button .../>
-or-
<Button>
    singleObject
</Button>
-or-
<Button ...>stringContent</Button>

-remarks

Tip

For more info, design guidance, and code examples, see Buttons.

The Button class represents the most basic type of button control. For info on other similar button controls that are more specialized, see HyperlinkButton or RepeatButton.

A standard button

Button interaction

When you tap a Button with a finger or stylus, or press a left mouse button while the pointer is over it, the button raises the Click event. If a button has keyboard focus, pressing the Enter key or the Spacebar key also raises the Click event.

You generally can't handle low-level PointerPressed events on a Button because it has the Click behavior instead. For more info, see Events and routed events overview.

You can change how a button raises the Click event by changing the ClickMode property. The default ClickMode value is Release. If ClickMode is Hover, the Click event can't be raised with the keyboard.

Button content

Button is a ContentControl. Its XAML content property is Content, and this enables a syntax like this for XAML: <Button>A button's content</Button>. For more info about XAML content properties, see XAML overview.

Button derived classes

Button is the parent class for AppBarButton.

Control style and template

You can modify the default Style and ControlTemplate to give the control a unique appearance. For information about modifying a control's style and template, see Styling controls. The default style, template, and resources that define the look of the control are included in the generic.xaml file. For design purposes, generic.xaml is available locally with the SDK or NuGet package installation.

  • WinUI Styles (recommended): For updated styles from WinUI, see \Users\<username>\.nuget\packages\microsoft.ui.xaml\<version>\lib\uap10.0\Microsoft.UI.Xaml\Themes\generic.xaml.
  • Non-WinUI styles: For built-in styles, see %ProgramFiles(x86)%\Windows Kits\10\DesignTime\CommonConfiguration\Neutral\UAP\<SDK version>\Generic\generic.xaml.

Locations might be different if you customized the installation. Styles and resources from different versions of the SDK might have different values.

XAML also includes resources that you can use to modify the colors of a control in different visual states without modifying the control template. Modifying these resources is preferred to setting properties such as Background and Foreground. For more info, see the Light-weight styling section of the XAML styles article. Light-weight styling resources are available starting in Windows 10, version 1607 (SDK 14393).

-examples

Tip

For more info, design guidance, and code examples, see Buttons.

[!div class="nextstepaction"] Open the WinUI 2 Gallery app and see the Button in action

The WinUI 2 Gallery app includes interactive examples of most WinUI 2 controls, features, and functionality. Get the app from the Microsoft Store or get the source code on GitHub.

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.

[!code-xaml1]

[!code-csharp11]

[!code-vb11]

-see-also

Buttons overview, AppBarButton, CheckBox, HyperlinkButton, RepeatButton, RadioButton, ToggleSwitch