Skip to main content
A standard button control that responds to user clicks and can execute commands.

Namespace

Avalonia.Controls

Inheritance

ControlTemplatedControlContentControlButton

Properties

ClickMode
ClickMode
Gets or sets a value indicating how the button reacts to clicks. Values: Release (default) or Press.
Command
ICommand?
Gets or sets an ICommand to be invoked when the button is clicked.
CommandParameter
object?
Gets or sets a parameter to be passed to the Command.
HotKey
KeyGesture?
Gets or sets a KeyGesture associated with this control.
IsDefault
bool
Gets or sets a value indicating whether the button is the default button for the window (activated by Enter key).
IsCancel
bool
Gets or sets a value indicating whether the button is the Cancel button for the window (activated by Escape key).
IsPressed
bool
Gets a value indicating whether the button is currently pressed. Read-only.
Flyout
FlyoutBase?
Gets or sets the Flyout that should be shown with this button.

Events

Click
EventHandler<RoutedEventArgs>
Raised when the user clicks the button.

Methods

OpenFlyout()
void
Opens the button’s flyout (if set).
CloseFlyout()
void
Closes the button’s flyout (if open).

Protected Methods

OnClick()
void
Invokes the Click event. Override to customize click behavior.
OnFlyoutOpened()
void
Invoked when the button’s flyout is opened. Override to add custom logic.
OnFlyoutClosed()
void
Invoked when the button’s flyout is closed. Override to add custom logic.

Usage

XAML Example - Basic Button

<Button Content="Click Me" 
        Click="Button_Click" />

XAML Example - Command Binding

<Button Content="Save" 
        Command="{Binding SaveCommand}"
        CommandParameter="{Binding CurrentItem}" />

XAML Example - Default/Cancel Buttons

<StackPanel>
  <Button Content="OK" 
          IsDefault="True"
          Command="{Binding OkCommand}" />
  <Button Content="Cancel" 
          IsCancel="True"
          Command="{Binding CancelCommand}" />
</StackPanel>

XAML Example - With Flyout

<Button Content="Options">
  <Button.Flyout>
    <MenuFlyout>
      <MenuItem Header="Option 1" />
      <MenuItem Header="Option 2" />
      <MenuItem Header="Option 3" />
    </MenuFlyout>
  </Button.Flyout>
</Button>

C# Example - Event Handler

var button = new Button
{
    Content = "Click Me"
};

button.Click += (sender, e) => 
{
    Debug.WriteLine("Button clicked!");
};

C# Example - Command Pattern

public class ViewModel : ViewModelBase
{
    public ICommand SaveCommand { get; }
    
    public ViewModel()
    {
        SaveCommand = ReactiveCommand.Create(() => 
        {
            // Save logic here
            SaveData();
        });
    }
    
    private void SaveData()
    {
        // Implementation
    }
}

// In view
var button = new Button
{
    Content = "Save",
    [!Button.CommandProperty] = new Binding("SaveCommand")
};

Hotkey Example

<Button Content="_Save" 
        HotKey="Ctrl+S"
        Command="{Binding SaveCommand}" />

Custom Click Mode

<Button Content="Press Me" 
        ClickMode="Press"
        Click="Button_Click" />

ClickMode Enum

Release
The Click event is raised when the pointer is released (default behavior).
Press
The Click event is raised when the pointer is pressed.

Source File

Button.cs:1-756

See Also

Build docs developers (and LLMs) love