Table of Contents

Class Button

Namespace
Avalonia.Controls
Assembly
Avalonia.Controls.dll

A standard button control.

[PseudoClasses(new string[] { ":flyout-open", ":pressed" })]
public class Button : ContentControl, INotifyPropertyChanged, IDataContextProvider, ILogical, IThemeVariantHost, IResourceHost, IResourceNode, IStyleHost, ISetLogicalParent, ISetInheritanceParent, ISupportInitialize, IStyleable, INamed, IInputElement, IDataTemplateHost, ISetterValue, ICommandSource
Inheritance
Button
Implements
Derived
Inherited Members
Extension Methods

Constructors

Button()

Initializes a new instance of the Button class.

public Button()

Fields

ClickEvent

Defines the Click event.

public static readonly RoutedEvent<RoutedEventArgs> ClickEvent

Field Value

RoutedEvent<RoutedEventArgs>

ClickModeProperty

Defines the ClickMode property.

public static readonly StyledProperty<ClickMode> ClickModeProperty

Field Value

StyledProperty<ClickMode>

CommandParameterProperty

Defines the CommandParameter property.

public static readonly StyledProperty<object?> CommandParameterProperty

Field Value

StyledProperty<object>

CommandProperty

Defines the Command property.

public static readonly StyledProperty<ICommand?> CommandProperty

Field Value

StyledProperty<ICommand>

FlyoutProperty

Defines the Flyout property

public static readonly StyledProperty<FlyoutBase?> FlyoutProperty

Field Value

StyledProperty<FlyoutBase>

HotKeyProperty

Defines the HotKey property.

public static readonly StyledProperty<KeyGesture?> HotKeyProperty

Field Value

StyledProperty<KeyGesture>

IsCancelProperty

Defines the IsCancel property.

public static readonly StyledProperty<bool> IsCancelProperty

Field Value

StyledProperty<bool>

IsDefaultProperty

Defines the IsDefault property.

public static readonly StyledProperty<bool> IsDefaultProperty

Field Value

StyledProperty<bool>

IsPressedProperty

Defines the IsPressed property.

public static readonly DirectProperty<Button, bool> IsPressedProperty

Field Value

DirectProperty<Button, bool>

Properties

ClickMode

Gets or sets a value indicating how the Button should react to clicks.

public ClickMode ClickMode { get; set; }

Property Value

ClickMode

Command

Gets or sets an ICommand to be invoked when the button is clicked.

public ICommand? Command { get; set; }

Property Value

ICommand

CommandParameter

Gets or sets a parameter to be passed to the Command.

public object? CommandParameter { get; set; }

Property Value

object

Flyout

Gets or sets the Flyout that should be shown with this button.

public FlyoutBase? Flyout { get; set; }

Property Value

FlyoutBase

HotKey

Gets or sets an KeyGesture associated with this control

public KeyGesture? HotKey { get; set; }

Property Value

KeyGesture

IsCancel

Gets or sets a value indicating whether the button is the Cancel button for the window.

public bool IsCancel { get; set; }

Property Value

bool

IsDefault

Gets or sets a value indicating whether the button is the default button for the window.

public bool IsDefault { get; set; }

Property Value

bool

IsEnabledCore

Allows a derived class to override the enabled state of the control.

protected override bool IsEnabledCore { get; }

Property Value

bool

Remarks

Derived controls may wish to disable the enabled state of the control without overwriting the user-supplied IsEnabled setting. This can be done by overriding this property to return the overridden enabled state. If the value returned from IsEnabledCore should change, then the derived control should call UpdateIsEffectivelyEnabled().

IsPressed

Gets or sets a value indicating whether the button is currently pressed.

public bool IsPressed { get; }

Property Value

bool

Methods

CloseFlyout()

Closes the button's flyout.

protected virtual void CloseFlyout()

OnAccessKey(RoutedEventArgs)

This method is used to execute the action on an effective IInputElement when a corresponding access key has been invoked. By default, the Focus() method is invoked with the NavigationMethod.Tab to indicate a visual focus adorner. Overwrite this method if other methods or additional functionality is needed when an item should receive the focus.

protected override void OnAccessKey(RoutedEventArgs e)

Parameters

e RoutedEventArgs

AccessKeyEventArgs are passed on to indicate if there are multiple matches or not.

OnApplyTemplate(TemplateAppliedEventArgs)

Called when the control's template is applied. In simple terms, this means the method is called just before the control is displayed.

protected override void OnApplyTemplate(TemplateAppliedEventArgs e)

Parameters

e TemplateAppliedEventArgs

The event args.

OnAttachedToLogicalTree(LogicalTreeAttachmentEventArgs)

Called when the styled element is added to a rooted logical tree.

protected override void OnAttachedToLogicalTree(LogicalTreeAttachmentEventArgs e)

Parameters

e LogicalTreeAttachmentEventArgs

The event args.

OnAttachedToVisualTree(VisualTreeAttachmentEventArgs)

Called when the control is added to a rooted visual tree.

protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)

Parameters

e VisualTreeAttachmentEventArgs

The event args.

OnClick()

Invokes the Click event.

protected virtual void OnClick()

OnCreateAutomationPeer()

Returns a new, type-specific AutomationPeer implementation for the control.

protected override AutomationPeer OnCreateAutomationPeer()

Returns

AutomationPeer

The type-specific AutomationPeer implementation.

OnDetachedFromLogicalTree(LogicalTreeAttachmentEventArgs)

Called when the styled element is removed from a rooted logical tree.

protected override void OnDetachedFromLogicalTree(LogicalTreeAttachmentEventArgs e)

Parameters

e LogicalTreeAttachmentEventArgs

The event args.

OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs)

Called when the control is removed from a rooted visual tree.

protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)

Parameters

e VisualTreeAttachmentEventArgs

The event args.

OnFlyoutClosed()

Invoked when the button's flyout is closed.

protected virtual void OnFlyoutClosed()

OnFlyoutOpened()

Invoked when the button's flyout is opened.

protected virtual void OnFlyoutOpened()

OnKeyDown(KeyEventArgs)

Called before the KeyDown event occurs.

protected override void OnKeyDown(KeyEventArgs e)

Parameters

e KeyEventArgs

The event args.

OnKeyUp(KeyEventArgs)

Called before the KeyUp event occurs.

protected override void OnKeyUp(KeyEventArgs e)

Parameters

e KeyEventArgs

The event args.

OnLostFocus(RoutedEventArgs)

Called before the LostFocus event occurs.

protected override void OnLostFocus(RoutedEventArgs e)

Parameters

e RoutedEventArgs

The event args.

OnPointerCaptureLost(PointerCaptureLostEventArgs)

Called before the PointerCaptureLost event occurs.

protected override void OnPointerCaptureLost(PointerCaptureLostEventArgs e)

Parameters

e PointerCaptureLostEventArgs

The event args.

OnPointerPressed(PointerPressedEventArgs)

Called before the PointerPressed event occurs.

protected override void OnPointerPressed(PointerPressedEventArgs e)

Parameters

e PointerPressedEventArgs

The event args.

OnPointerReleased(PointerReleasedEventArgs)

Called before the PointerReleased event occurs.

protected override void OnPointerReleased(PointerReleasedEventArgs e)

Parameters

e PointerReleasedEventArgs

The event args.

OnPropertyChanged(AvaloniaPropertyChangedEventArgs)

Called when a avalonia property changes on the object.

protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)

Parameters

change AvaloniaPropertyChangedEventArgs

The property change details.

OpenFlyout()

Opens the button's flyout.

protected virtual void OpenFlyout()

UpdateDataValidation(AvaloniaProperty, BindingValueType, Exception?)

Called to update the validation state for properties for which data validation is enabled.

protected override void UpdateDataValidation(AvaloniaProperty property, BindingValueType state, Exception? error)

Parameters

property AvaloniaProperty

The property.

state BindingValueType

The current data binding state.

error Exception

The current data binding error, if any.

Events

Click

Raised when the user clicks the button.

public event EventHandler<RoutedEventArgs>? Click

Event Type

EventHandler<RoutedEventArgs>