Table of Contents

Class AvaloniaProperty

Namespace
Avalonia
Assembly
Avalonia.Base.dll

Base class for avalonia properties.

public abstract class AvaloniaProperty : IEquatable<AvaloniaProperty>, IPropertyInfo
Inheritance
AvaloniaProperty
Implements
Derived
Inherited Members

Fields

UnsetValue

Represents an unset property value.

public static readonly object UnsetValue

Field Value

object

Properties

Changed

Gets an observable that is fired when this property changes on any AvaloniaObject instance.

public IObservable<AvaloniaPropertyChangedEventArgs> Changed { get; }

Property Value

IObservable<AvaloniaPropertyChangedEventArgs>

An observable that is fired when this property changes on any AvaloniaObject instance.

Inherits

Gets a value indicating whether the property inherits its value.

public bool Inherits { get; }

Property Value

bool

IsAttached

Gets a value indicating whether this is an attached property.

public bool IsAttached { get; }

Property Value

bool

IsDirect

Gets a value indicating whether this is a direct property.

public bool IsDirect { get; }

Property Value

bool

IsReadOnly

Gets a value indicating whether this is a readonly property.

public bool IsReadOnly { get; }

Property Value

bool

Name

Gets the name of the property.

public string Name { get; }

Property Value

string

OwnerType

Gets the type of the class that registered the property.

public Type OwnerType { get; }

Property Value

Type

PropertyType

Gets the type of the property's value.

public Type PropertyType { get; }

Property Value

Type

Methods

Bind()

Returns a binding accessor that can be passed to AvaloniaObject's [] operator to initiate a binding.

public IndexerDescriptor Bind()

Returns

IndexerDescriptor

A IndexerDescriptor.

Remarks

The ! and ~ operators are short forms of this.

Equals(AvaloniaProperty?)

public bool Equals(AvaloniaProperty? other)

Parameters

other AvaloniaProperty

Returns

bool

Equals(object?)

public override bool Equals(object? obj)

Parameters

obj object

Returns

bool

GetHashCode()

public override int GetHashCode()

Returns

int

GetMetadata(AvaloniaObject)

Gets the AvaloniaPropertyMetadata which applies to this property when it is used with the specified object.

public AvaloniaPropertyMetadata GetMetadata(AvaloniaObject owner)

Parameters

owner AvaloniaObject

The object for which to retrieve metadata.

Returns

AvaloniaPropertyMetadata

GetMetadata(Type)

Gets the AvaloniaPropertyMetadata which applies to this property when it is used with the specified type.

public AvaloniaPropertyMetadata GetMetadata(Type type)

Parameters

type Type

The type for which to retrieve metadata.

Returns

AvaloniaPropertyMetadata

Remarks

For performance, prefer the GetMetadata(AvaloniaObject) overload when possible.

GetMetadata<T>()

Gets the AvaloniaPropertyMetadata which applies to this property when it is used with the specified type.

public AvaloniaPropertyMetadata GetMetadata<T>() where T : AvaloniaObject

Returns

AvaloniaPropertyMetadata

Type Parameters

T

The type for which to retrieve metadata.

IsValidValue(object?)

Checks whether the value is valid for the property.

public bool IsValidValue(object? value)

Parameters

value object

The value.

Returns

bool

True if the value is valid, otherwise false.

RegisterAttached<THost, TValue>(string, Type, TValue, bool, BindingMode, Func<TValue, bool>?, Func<AvaloniaObject, TValue, TValue>?)

Registers an attached AvaloniaProperty.

public static AttachedProperty<TValue> RegisterAttached<THost, TValue>(string name, Type ownerType, TValue defaultValue = default, bool inherits = false, BindingMode defaultBindingMode = BindingMode.OneWay, Func<TValue, bool>? validate = null, Func<AvaloniaObject, TValue, TValue>? coerce = null) where THost : AvaloniaObject

Parameters

name string

The name of the property.

ownerType Type

The type of the class that is registering the property.

defaultValue TValue

The default value of the property.

inherits bool

Whether the property inherits its value.

defaultBindingMode BindingMode

The default binding mode for the property.

validate Func<TValue, bool>

A value validation callback.

coerce Func<AvaloniaObject, TValue, TValue>

A value coercion callback.

Returns

AttachedProperty<TValue>

A AvaloniaProperty<TValue>

Type Parameters

THost

The type of the class that the property is to be registered on.

TValue

The type of the property's value.

Remarks

⚠️ Be cautious: if defaultValue is a reference type (like a list), this same instance will be shared across all controls using this property.
Use value types (like int, bool) or immutable objects only.
For collections or mutable types, use a factory pattern instead.

RegisterAttached<TOwner, THost, TValue>(string, TValue, bool, BindingMode, Func<TValue, bool>?, Func<AvaloniaObject, TValue, TValue>?)

Registers an attached AvaloniaProperty.

public static AttachedProperty<TValue> RegisterAttached<TOwner, THost, TValue>(string name, TValue defaultValue = default, bool inherits = false, BindingMode defaultBindingMode = BindingMode.OneWay, Func<TValue, bool>? validate = null, Func<AvaloniaObject, TValue, TValue>? coerce = null) where THost : AvaloniaObject

Parameters

name string

The name of the property.

defaultValue TValue

The default value of the property.

inherits bool

Whether the property inherits its value.

defaultBindingMode BindingMode

The default binding mode for the property.

validate Func<TValue, bool>

A value validation callback.

coerce Func<AvaloniaObject, TValue, TValue>

A value coercion callback.

Returns

AttachedProperty<TValue>

A AvaloniaProperty<TValue>

Type Parameters

TOwner

The type of the class that is registering the property.

THost

The type of the class that the property is to be registered on.

TValue

The type of the property's value.

Remarks

⚠️ Be cautious: if defaultValue is a reference type (like a list), this same instance will be shared across all controls using this property.
Use value types (like int, bool) or immutable objects only.
For collections or mutable types, use a factory pattern instead.

RegisterDirect<TOwner, TValue>(string, Func<TOwner, TValue>, Action<TOwner, TValue>?, TValue, BindingMode, bool)

Registers a direct AvaloniaProperty.

public static DirectProperty<TOwner, TValue> RegisterDirect<TOwner, TValue>(string name, Func<TOwner, TValue> getter, Action<TOwner, TValue>? setter = null, TValue unsetValue = default, BindingMode defaultBindingMode = BindingMode.OneWay, bool enableDataValidation = false) where TOwner : AvaloniaObject

Parameters

name string

The name of the property.

getter Func<TOwner, TValue>

Gets the current value of the property.

setter Action<TOwner, TValue>

Sets the value of the property.

unsetValue TValue

The value to use when the property is cleared. ⚠️ Be careful with reference types: if you pass a mutable object (like a list), it will be shared across all instances using this property. Use value types or immutable objects.

defaultBindingMode BindingMode

The default binding mode for the property.

enableDataValidation bool

Whether the property is interested in data validation.

Returns

DirectProperty<TOwner, TValue>

A AvaloniaProperty<TValue>

Type Parameters

TOwner

The type of the class that is registering the property.

TValue

The type of the property's value.

Register<TOwner, TValue>(string, TValue, bool, BindingMode, Func<TValue, bool>?, Func<AvaloniaObject, TValue, TValue>?, bool)

Registers a AvaloniaProperty.

public static StyledProperty<TValue> Register<TOwner, TValue>(string name, TValue defaultValue = default, bool inherits = false, BindingMode defaultBindingMode = BindingMode.OneWay, Func<TValue, bool>? validate = null, Func<AvaloniaObject, TValue, TValue>? coerce = null, bool enableDataValidation = false) where TOwner : AvaloniaObject

Parameters

name string

The name of the property.

defaultValue TValue

The default value of the property.

inherits bool

Whether the property inherits its value.

defaultBindingMode BindingMode

The default binding mode for the property.

validate Func<TValue, bool>

A value validation callback.

coerce Func<AvaloniaObject, TValue, TValue>

A value coercion callback.

enableDataValidation bool

Whether the property is interested in data validation.

Returns

StyledProperty<TValue>

A StyledProperty<TValue>

Type Parameters

TOwner

The type of the class that is registering the property.

TValue

The type of the property's value.

Remarks

⚠️ Be cautious: if defaultValue is a reference type (like a list), this same instance will be shared across all controls using this property.
Use value types (like int, bool) or immutable objects only.
For collections or mutable types, use a factory pattern instead.

ToString()

Gets the string representation of the property.

public override string ToString()

Returns

string

The property's string representation.

Unregister(Type)

public void Unregister(Type type)

Parameters

type Type

Operators

operator ==(AvaloniaProperty?, AvaloniaProperty?)

Tests two AvaloniaPropertys for equality.

public static bool operator ==(AvaloniaProperty? a, AvaloniaProperty? b)

Parameters

a AvaloniaProperty

The first property.

b AvaloniaProperty

The second property.

Returns

bool

True if the properties are equal, otherwise false.

operator !=(AvaloniaProperty?, AvaloniaProperty?)

Tests two AvaloniaPropertys for inequality.

public static bool operator !=(AvaloniaProperty? a, AvaloniaProperty? b)

Parameters

a AvaloniaProperty

The first property.

b AvaloniaProperty

The second property.

Returns

bool

True if the properties are equal, otherwise false.

operator !(AvaloniaProperty)

Provides access to a property's binding via the AvaloniaObject indexer.

public static IndexerDescriptor operator !(AvaloniaProperty property)

Parameters

property AvaloniaProperty

The property.

Returns

IndexerDescriptor

A IndexerDescriptor describing the binding.

operator ~(AvaloniaProperty)

Provides access to a property's template binding via the AvaloniaObject indexer.

public static IndexerDescriptor operator ~(AvaloniaProperty property)

Parameters

property AvaloniaProperty

The property.

Returns

IndexerDescriptor

A IndexerDescriptor describing the binding.