AvaloniaPropertys for your control. Avalonia properties consist of two parts: the property definition and the CLR getter/setter for the property.
AvaloniaProperty.Registerand storing the result in a
static readonlyfield. You then create a standard C# property to access it.
Bordercontrol defines its
AvaloniaProperty.Registermethod also accepts a number of other parameters:
defaultValue: This gives the property a default value. Be sure to only pass value types and immutable types here as passing a reference type will cause the same object to be used on all instances on which the property is registered.
inherits: Specified that the property's default value should come from the parent control.
defaultBindingMode: The default binding mode for the property. Can be set to
validate: A validation/coercion function of type
Func<TOwner, TValue, TValue>. The function accepts the instance of the class on which the property is being set and the value and returns the coerced value or throws an exception for an invalid value.
DependencyPropertyin other XAML frameworks.
StyledPropertyon Another Class
Backgroundbeing a good example. To register a property defined on another control, you call
AvaloniaProperty.RegisterDirectmethod. Here is how
Visualregisters the readonly
SetAndRaiseis used to notify listeners to changes to the property.
RegisterAttachedmethod and their accessors are defined as static methods.
RegisterDirectisn't just used for registering readonly properties. You can also pass a setter to
RegisterDirectto expose a standard C# property as a Avalonia property.
StyledPropertywhich is registered using
AvaloniaProperty.Registermaintains a prioritized list of values and bindings that allow styles to work. However, this is overkill for many properties, such as
ItemsControl.Items- this will never be styled and the overhead involved with styled properties is unnecessary.
AddOwneron a styled property, you can also add an owner to a direct property. Because direct properties reference fields on the control, you must also add a field for the property: