Binding
TheBinding class provides data binding functionality in Avalonia UI, allowing you to connect UI elements to data sources with automatic change notification.
In Avalonia 11.x and later,
Binding is a compatibility wrapper around ReflectionBinding. For new code, consider using compiled bindings for better performance.Namespace
Inheritance
Constructors
Creates a new instance of the Binding class with no path.
Creates a new instance of the Binding class with the specified property path.Parameters:
path(string): The property path to bind to
Properties
Gets or sets the path to the property on the source object.Example:
"Name", "User.Address.City", "Items[0].Title"Gets or sets the binding mode. Default is
BindingMode.Default.Values:OneWay- Updates target when source changesTwoWay- Updates both target and source when either changesOneTime- Sets target once from sourceOneWayToSource- Updates source when target changesDefault- Uses the property’s default binding mode
Gets or sets the source object for the binding. If not set, uses the DataContext.
Gets or sets the binding source relative to the target element.Examples:
RelativeSource.Self- Binds to the element itselfRelativeSource.TemplatedParent- Binds to the templated parent
Gets or sets the name of the element to use as the binding source.
Gets or sets the value converter to use for converting between source and target types.
Gets or sets the parameter to pass to the converter.
Gets or sets the format string to use when converting the value to a string.Example:
"{0:C}" for currency formattingGets or sets the value to use when the binding cannot produce a value.
Gets or sets the value to use when the source value is null.
Usage Examples
Basic XAML Binding
Two-Way Binding
Binding with Converter
ElementName Binding
Code-Behind Binding
Binding to Self
Compiled Bindings
For better performance and compile-time checking, use compiled bindings:Compiled bindings are generated at compile time and don’t use reflection, resulting in better performance and type safety.