Skip to main content

Register<TOwner, TValue> Method

Registers a AvaloniaProperty.

Definition

Namespace: Avalonia
Assembly: Avalonia.Base (in Avalonia.Base.dll)

public static StyledProperty<TValue> Register<TOwner, TValue>(
string name,
TValue defaultValue = null,
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

  String
The name of the property.
  TValue  (Optional)
The default value of the property.
  Boolean  (Optional)
Whether the property inherits its value.
  BindingMode  (Optional)
The default binding mode for the property.
  Func(TValue, Boolean)  (Optional)
A value validation callback.
  Func(AvaloniaObject, TValue, TValue)  (Optional)
A value coercion callback.
  Boolean  (Optional)
Whether the property is interested in data validation.

Type Parameters

The type of the class that is registering the property.
The type of the property's value.

Return Value

StyledProperty(TValue)
A StyledProperty(TValue)

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.

See Also

Reference

AvaloniaProperty Class
Avalonia Namespace