Data context
When Avalonia performs data binding, it must locate an application object to bind to. This location is represented by a data context.
Overview
Every control in Avalonia has a DataContext property, whether built-in controls, user controls, or windows.
When binding, Avalonia performs a hierarchical search of the logical control tree, starting with the control where you define the binding, until it finds a data context to use.
This means that a control defined in a window can use the data context of the window. Or, as above, a control in a control in a window can use the window's data context.
For information about the control trees in Avalonia, and how to see them at run-time, see Control trees.
New projects
Main window data context
New projects created with the Avalonia MVVM Application template automatically include a MainWindow whose data context is set to MainViewModel. You can find this in the App.axaml.cs file:
public override void OnFrameworkInitializationCompleted()
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
desktop.MainWindow = new MainWindow
{
DataContext = new MainViewModel(),
};
}
base.OnFrameworkInitializationCompleted();
}
Data context objects
Objects included in the main window's data context can be found in the file MainViewModel.cs:
public partial class MainViewModel : ViewModelBase
{
[ObservableProperty] public partial string Greeting { get; set; } = "Welcome to Avalonia!";
}
Binding to the data context
In the main window file MainWindow.axaml, you can see that the window's content area contains a TextBox that uses a Text property bound to Greeting.
When the project runs, the data binder searches up the logical control tree from the text box and finds a data context set at the main window level. So the bound text appears as "Welcome to Avalonia!", as defined in the main view model.
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="using:AvaloniaMVVMApplication2.ViewModels"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="AvaloniaMVVMApplication2.Views.MainWindow"
x:DataType="vm:MainViewModel"
Icon="/Assets/avalonia-logo.ico"
Title="AvaloniaMVVMApplication2">
<Design.DataContext>
<vm:MainViewModel />
</Design.DataContext>
<TextBox Text="{Binding Greeting}"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
</Window>
Setting the data context
Avalonia does not automatically assign a control's data context. Because of this, any bindings that require a source DataContext do not work until it is set.
As described above, new projects started with the Avalonia MVVM Application template automatically set MainViewModel as the data context for MainWindow in the App.axaml.cs file. Any additional window you create needs its own data context.
There are three ways to set the data context of a new window:
In the code that creates a new window
Set DataContext in the object initializer, so that a new window gets a data context when it is created.
Because the caller chooses the view model, the window doesn't need to know how to construct one. This is suitable for view models that take constructor arguments, or that come from a dependency injection container.
var window = new MyWindow
{
DataContext = new MyViewModel()
};
window.Show();
In the window's constructor
Set DataContext in the window's code-behind file, after calling InitializeComponent().
When data context is set in the code-behind, the window always creates its own view model, wherever it is opened from. The view is therefore tied to a single view model.
public partial class MyWindow : Window
{
public MyWindow()
{
InitializeComponent();
DataContext = new MyViewModel();
}
}
In XAML
Set the data context in the window's XAML as a property element, i.e., <Window.DataContext>.
This has a similar effect as setting it in the constructor, but requires the view model to have a parameterless constructor.
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="using:MyApp.ViewModels"
x:Class="MyApp.Views.MyWindow"
x:DataType="vm:MyViewModel"
Title="MyWindow">
<Window.DataContext>
<vm:MyViewModel />
</Window.DataContext>
<TextBlock Text="{Binding UserName}" />
</Window>
See Binding debugging for how to debug issues that can cause DataContext to be null.
Notes
x:DataTypedoes not set the data context. It only tells the XAML compiler what type to expect for compiled bindings at build time.- New windows do not inherit data context, even if the new window is opened with
Show(owner)and is a child window of the owner window. This is because eachWindowis the root of its own logical tree. - User controls, conversely, do inherit the data context of the window that hosts them, as they are part of the window's logical tree.
Design-time data context
Avalonia can set a design-time data context for a control. This means the control can display real binding data in the preview pane while you adjust layout and styles, allowing you to carry out UI design without building the project repeatedly.
You can set the design-time data context as a property element in XAML. By default, the Avalonia MVVM Application template creates new projects with MainViewModel as the design-time data context.
For further guidance on using the design-time data context, see XAML preview and design settings.
<Design.DataContext>
<vm:MainViewModel />
</Design.DataContext>
The design-time data context applies only in the previewer. During a real build, the XAML compiler removes Design.DataContext and d:DataContext. A window's build-time data context is not automatically used as the design-time data context, and vice versa.
See also
- Introduction to data binding: Data binding overview.
- Data binding syntax: Binding paths, modes, and converters.
- XAML preview and design settings: Design-time data context configuration.
- Binding debugging: Diagnose data binding issues.
- The MVVM pattern