Skip to main content

Introduction to data binding

Avalonia uses data binding to move data from application objects into UI controls, change the data in application objects in response to user input, and initiate actions on the application objects in response to commands from the user.

Control
Update
Object
Notify
Control (Binding Target): The UI control that displays data. Examples include TextBox, TextBlock, and other Avalonia controls with bindable properties.

In this arrangement, the control is the binding target, and the object is the data source.

Avalonia runs a data binding system to complete much of the above activity from simple mappings declared in the XAML; that is without requiring you to add a lot of additional coding.

Data binding mappings are defined using XML between the attributes of an Avalonia control, and the properties of an application object. In general terms, the syntax is like this:

<SomeControl Attribute="{Binding PropertyName}" />

The mappings can be bidirectional: where changes in the properties of a bound application object are reflected in the control, and changes in the control (however caused) are applied to the underlying object. An example of bidirectional binding is a text input bound to a string property of an object. The XML might look like this:

<TextBox Text="{Binding FirstName}" />

If the user edits the text in the text box, then the FirstName property of the underlying object is automatically updated. In the other direction, if the FirstName property of the underlying object changes, then the text visible in the text box is updated.

Bindings can be unidirectional: where changes in the properties of a bound application object are reflected in the control, but the user cannot change the control. An example of this would be the text block control, which is read-only.

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

Binding is used with the MVVM architectural pattern, and this is one of the principle ways of programming with Avalonia UI.

info

For more information about how to use the MVVM Pattern with Avalonia, see The MVVM Pattern.

info

For background information on the origins and development of the MVVM pattern at Microsoft, see the Microsoft Patterns and Practices article.

Binding modes

Bindings can operate in different modes that control how data flows:

ModeDescription
OneWaySource changes update the target. Target changes are not sent back.
TwoWayChanges in either source or target update the other.
OneTimeThe source value is read once and does not track subsequent property changes. The binding re-evaluates if the DataContext changes.
OneWayToSourceTarget changes update the source, but not vice versa.
DefaultThe mode is determined by the target property. Most display properties default to OneWay; editable properties like TextBox.Text default to TwoWay.
<TextBox Text="{Binding Name, Mode=TwoWay}" />
<TextBlock Text="{Binding Name, Mode=OneWay}" />

FallbackValue and TargetNullValue

PropertyDescription
FallbackValueValue displayed when the binding cannot resolve (e.g., property not found).
TargetNullValueValue displayed when the source property is null.
<TextBlock Text="{Binding Description, TargetNullValue='No description available'}" />
<Image Source="{Binding AvatarUrl, FallbackValue={StaticResource DefaultAvatar}}" />

See also