Avalonia property system

From linuxdevel/Avalonia-skills · View interactive version

Avalonia property system is an AI agent skill from the GitHub repository linuxdevel/Avalonia-skills. Use when working with Avalonia's property system: StyledProperty, DirectProperty, AttachedProperty, property inheritance, metadata override, coercion, value priority, or observing property changes via GetObservable or OnPropertyChanged.

How to use this skill

Skill file

Avalonia Property System

Overview

AvaloniaProperty is the foundation of Avalonia's data binding, styling, and animations. Three kinds:

StyledProperty

public static readonly StyledProperty<IBrush?> BackgroundProperty =
    AvaloniaProperty.Register<MyControl, IBrush?>(
        name: nameof(Background),
        defaultValue: null,
        inherits: false,
        defaultBindingMode: BindingMode.OneWay,
        validate: null,
        coerce: CoerceBackground);

public IBrush? Background
{
    get => GetValue(BackgroundProperty);
    set => SetValue(BackgroundProperty, value);
}

private static IBrush? CoerceBackground(AvaloniaObject instance, IBrush? value)
    => value ?? Brushes.Transparent;

Inherit/reuse from another class:

// Re-register an existing property as owner
public static readonly StyledProperty<IBrush?> BackgroundProperty =
    Border.BackgroundProperty.AddOwner<MyPanel>();

DirectProperty

Use for properties that back observable collections, streams, or are performance-sensitive:

public static readonly DirectProperty<MyControl, string> TextProperty =
    AvaloniaProperty.RegisterDirect<MyControl, string>(
        name: nameof(Text),
        getter: o => o.Text,
        setter: (o, v) => o.Text = v,
        defaultBindingMode: BindingMode.TwoWay,
        enableDataValidation: true);

private string _text = "";
public string Text
{
    get => _text;
    set => SetAndRaise(TextProperty, ref _text, value);
}

SetAndRaise performs equality check, updates field, raises PropertyChanged.

AttachedProperty

public static class Grid
{
    public static readonly AttachedProperty<int> RowProperty =
        AvaloniaProperty.RegisterAttached<Grid, Control, int>(
            name: "Row",
            defaultValue: 0,
            inherits: false);

    public static int GetRow(Control element) => element.GetValue(RowProperty);
    public static void SetRow(Control element, int value) => element.SetValue(RowProperty, value);
}

XAML usage:

<TextBlock Grid.Row="1" Grid.Column="2"/>

Property Value Priority (highest → lowest)

| Priority | Source |

|---|---|

| 1 | Animation |

| 2 | Local value (SetValue / binding at LocalValue) |

| 3 | Style trigger (active pseudoclass setter) |

| 4 | Template binding (BindingPriority.Template; 11.x name TemplatedParent was removed in 12) |

| 5 | Style setter (non-triggered) |

| 6 | Inherited value |

| 7 | Default value |

SetCurrentValue — sets without overriding bindings/styles (priority: LocalValue but clears on style change):

// Doesn't break two-way binding
myControl.SetCurrentValue(IsCheckedProperty, true);

Observing Property Changes

On a specific instance (reactive):

myTextBox
    .GetObservable(TextBox.TextProperty)
    .Subscribe(text => Console.WriteLine($"Text: {text}"));

// With debounce (requires System.Reactive)
myTextBox
    .GetObservable(TextBox.TextProperty)
    .Throttle(TimeSpan.FromMilliseconds(300))
    .ObserveOn(RxApp.MainThreadScheduler)
    .Subscribe(HandleTextChanged);

In a control, override OnPropertyChanged:

protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
    base.OnPropertyChanged(change);

    if (change.Property == ValueProperty)
    {
        var newValue = change.GetNewValue<int>();
        var oldValue = change.GetOldValue<int>();
        UpdateVisuals(newValue);
    }
}

Static class handler (fires for all instances):

static MyControl()
{
    ValueProperty.Changed.AddClassHandler<MyControl>((control, e) =>
        control.OnValueChanged(e));

    // Or multiple properties
    AffectsRender<MyControl>(ForegroundProperty, BackgroundProperty, PaddingProperty);
    AffectsMeasure<MyControl>(WidthProperty, HeightProperty);
    AffectsArrange<MyControl>(AlignmentProperty);
}

AffectsRender / AffectsMeasure / AffectsArrange — declare layout/render dependencies efficiently.

Inherited Properties

public static readonly StyledProperty<FlowDirection> FlowDirectionProperty =
    AvaloniaProperty.Register<Control, FlowDirection>(
        nameof(FlowDirection),
        defaultValue: FlowDirection.LeftToRight,
        inherits: true);

Child controls inherit the value from their nearest ancestor that sets it explicitly.

Metadata Override

Override default value or coerce function for a subclass:

static MyControl()
{
    BackgroundProperty.OverrideMetadata<MyControl>(
        new StyledPropertyMetadata<IBrush?>(
            defaultValue: Brushes.White,
            defaultBindingMode: BindingMode.OneWay,
            coerce: MyCoerce));
}

Data Validation

Enable on a property with enableDataValidation: true, then throw in setter or implement INotifyDataErrorInfo. (12.0+) such properties report validation errors automatically; remove UpdateDataValidation overrides that only call DataValidationErrors.SetError. To keep custom handling, override UpdateDataValidation(AvaloniaProperty, BindingValueType, Exception?) and skip the base call for that property. Data-annotations validation is opt-in in 12 (on by default in 11.x).

private int _age;
public int Age
{
    get => _age;
    set
    {
        if (value < 0 || value > 150)
            throw new ArgumentOutOfRangeException(nameof(Age), "Age must be 0–150");
        SetAndRaise(AgeProperty, ref _age, value);
    }
}

Common Mistakes

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.