Class AvaloniaProperty
- Namespace
- Avalonia
- Assembly
- Avalonia.Base.dll
Base class for avalonia properties.
public abstract class AvaloniaProperty : IEquatable<AvaloniaProperty>, IPropertyInfo
- Inheritance
-
AvaloniaProperty
- Implements
- Derived
-
AvaloniaProperty<TValue>
- Inherited Members
Fields
UnsetValue
Represents an unset property value.
public static readonly object UnsetValue
Field Value
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
IsAttached
Gets a value indicating whether this is an attached property.
public bool IsAttached { get; }
Property Value
IsDirect
Gets a value indicating whether this is a direct property.
public bool IsDirect { get; }
Property Value
IsReadOnly
Gets a value indicating whether this is a readonly property.
public bool IsReadOnly { get; }
Property Value
Name
Gets the name of the property.
public string Name { get; }
Property Value
OwnerType
Gets the type of the class that registered the property.
public Type OwnerType { get; }
Property Value
PropertyType
Gets the type of the property's value.
public Type PropertyType { get; }
Property Value
Methods
Bind()
Returns a binding accessor that can be passed to AvaloniaObject's [] operator to initiate a binding.
public IndexerDescriptor Bind()
Returns
Remarks
The ! and ~ operators are short forms of this.
Equals(AvaloniaProperty?)
public bool Equals(AvaloniaProperty? other)
Parameters
other
AvaloniaProperty
Returns
Equals(object?)
public override bool Equals(object? obj)
Parameters
obj
object
Returns
GetHashCode()
public override int GetHashCode()
Returns
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
AvaloniaObjectThe object for which to retrieve metadata.
Returns
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
TypeThe type for which to retrieve metadata.
Returns
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
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
objectThe 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
stringThe name of the property.
ownerType
TypeThe type of the class that is registering the property.
defaultValue
TValueThe default value of the property.
inherits
boolWhether the property inherits its value.
defaultBindingMode
BindingModeThe 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>
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
stringThe name of the property.
defaultValue
TValueThe default value of the property.
inherits
boolWhether the property inherits its value.
defaultBindingMode
BindingModeThe 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>
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
stringThe 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
TValueThe 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
BindingModeThe default binding mode for the property.
enableDataValidation
boolWhether the property is interested in data validation.
Returns
- DirectProperty<TOwner, 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
stringThe name of the property.
defaultValue
TValueThe default value of the property.
inherits
boolWhether the property inherits its value.
defaultBindingMode
BindingModeThe default binding mode for the property.
validate
Func<TValue, bool>A value validation callback.
coerce
Func<AvaloniaObject, TValue, TValue>A value coercion callback.
enableDataValidation
boolWhether the property is interested in data validation.
Returns
- 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
AvaloniaPropertyThe first property.
b
AvaloniaPropertyThe 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
AvaloniaPropertyThe first property.
b
AvaloniaPropertyThe 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
AvaloniaPropertyThe 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
AvaloniaPropertyThe property.
Returns
- IndexerDescriptor
A IndexerDescriptor describing the binding.