Skip to main content

Binding

The Binding 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

Avalonia.Data

Inheritance

Object → ReflectionBinding → Binding

Constructors

Binding()
constructor
Creates a new instance of the Binding class with no path.
Binding(string path)
constructor
Creates a new instance of the Binding class with the specified property path.Parameters:
  • path (string): The property path to bind to

Properties

Path
string
Gets or sets the path to the property on the source object.Example: "Name", "User.Address.City", "Items[0].Title"
Mode
BindingMode
Gets or sets the binding mode. Default is BindingMode.Default.Values:
  • OneWay - Updates target when source changes
  • TwoWay - Updates both target and source when either changes
  • OneTime - Sets target once from source
  • OneWayToSource - Updates source when target changes
  • Default - Uses the property’s default binding mode
Source
object
Gets or sets the source object for the binding. If not set, uses the DataContext.
RelativeSource
RelativeSource
Gets or sets the binding source relative to the target element.Examples:
  • RelativeSource.Self - Binds to the element itself
  • RelativeSource.TemplatedParent - Binds to the templated parent
ElementName
string
Gets or sets the name of the element to use as the binding source.
Converter
IValueConverter
Gets or sets the value converter to use for converting between source and target types.
ConverterParameter
object
Gets or sets the parameter to pass to the converter.
StringFormat
string
Gets or sets the format string to use when converting the value to a string.Example: "{0:C}" for currency formatting
FallbackValue
object
Gets or sets the value to use when the binding cannot produce a value.
TargetNullValue
object
Gets or sets the value to use when the source value is null.

Usage Examples

Basic XAML Binding

<TextBlock Text="{Binding Name}" />

Two-Way Binding

<TextBox Text="{Binding UserName, Mode=TwoWay}" />

Binding with Converter

<TextBlock Text="{Binding Age, Converter={StaticResource AgeToStringConverter}}" />

ElementName Binding

<Slider Name="slider" Minimum="0" Maximum="100" />
<TextBlock Text="{Binding #slider.Value}" />

Code-Behind Binding

using Avalonia.Data;

// Create a binding
var binding = new Binding("Name")
{
    Mode = BindingMode.TwoWay,
    Converter = new MyConverter(),
    FallbackValue = "Unknown"
};

// Apply the binding
textBlock.Bind(TextBlock.TextProperty, binding);

Binding to Self

var binding = new Binding("Value")
{
    RelativeSource = RelativeSource.Self
};

Compiled Bindings

For better performance and compile-time checking, use compiled bindings:
<Window xmlns:vm="using:MyApp.ViewModels"
        x:DataType="vm:MainViewModel">
    <TextBlock Text="{Binding Name}" />
</Window>
Compiled bindings are generated at compile time and don’t use reflection, resulting in better performance and type safety.

See Also

Build docs developers (and LLMs) love