Skip to main content
Base class for Avalonia controls. Extends InputElement and adds features like focus management, context menus, data templates, and lifecycle events.

Namespace

Avalonia.Controls

Inheritance

InputElementControl

Properties

FocusAdorner
ITemplate<Control>?
Gets or sets the control’s focus adorner template.
Tag
object?
Gets or sets a user-defined object attached to the control. Useful for storing custom data.
ContextMenu
ContextMenu?
Gets or sets a context menu for the control.
ContextFlyout
FlyoutBase?
Gets or sets a context flyout for the control.
IsLoaded
bool
Gets a value indicating whether the control is fully constructed in the visual tree and both layout and render are complete. Read-only.
DataTemplates
DataTemplates
Gets or sets the data templates for the control. Each control may define data templates which are applied to the control itself and its children. Read-only collection.

Events

Loaded
EventHandler<RoutedEventArgs>
Occurs when the control has been fully constructed in the visual tree and both layout and render are complete. This event is guaranteed to occur after the control template is applied.
Unloaded
EventHandler<RoutedEventArgs>
Occurs when the control is removed from the visual tree.
SizeChanged
EventHandler<SizeChangedEventArgs>
Occurs when the bounds (actual size) of the control have changed.

Protected Methods

GetTemplateFocusTarget()
Control?
Gets the element that receives the focus adorner. Returns the control itself by default. Override to customize focus target behavior.
OnLoaded(RoutedEventArgs)
void
Raises the Loaded event. Override to add custom logic when the control is loaded.
OnUnloaded(RoutedEventArgs)
void
Raises the Unloaded event. Override to add custom logic when the control is unloaded.
OnSizeChanged(SizeChangedEventArgs)
void
Raises the SizeChanged event. Override to respond to size changes.
OnCreateAutomationPeer()
AutomationPeer
Returns a new, type-specific AutomationPeer implementation for the control. Override to provide custom automation support.

Usage

XAML Example

<Control Tag="CustomData" 
         Loaded="OnControlLoaded">
  <Control.ContextMenu>
    <ContextMenu>
      <MenuItem Header="Option 1" />
      <MenuItem Header="Option 2" />
    </ContextMenu>
  </Control.ContextMenu>
</Control>

C# Example

var control = new Control
{
    Tag = "MyCustomData"
};

control.Loaded += (sender, e) => 
{
    // Control is fully loaded and ready
    Debug.WriteLine("Control loaded!");
};

control.SizeChanged += (sender, e) => 
{
    Debug.WriteLine($"Size changed from {e.PreviousSize} to {e.NewSize}");
};

// Access Tag later
var data = control.Tag as string;

Checking Load State

if (control.IsLoaded)
{
    // Perform actions that require the control to be fully loaded
    ProcessControl(control);
}

Source File

Control.cs:1-559

See Also

Build docs developers (and LLMs) love