The DataContext

The Control.DataContext property describes where controls will look by default for values when binding. The data context will usually be set for top-level controls such as Window and child controls will inherit this data context.

If you created your application with the Avalonia MVVM Application template then you will see something like this in your Program.cs file:

static void Main(string[] args)
    BuildAvaloniaApp().Start<MainWindow>(() => new MainWindowViewModel());

What this piece of code means is that when the MainWindow is created, a new instance of MainWindowViewModel will be created and assigned to the window's DataContext property. From here all bindings will by default bind to properties on this object.

You can bind child controls to properties on the DataContext too, for example:

  <Button DataContext="{Binding ChildProperty}">

Will bind the Button's DataContext to Window.DataContext.ChildProperty.

Note that when binding to DataContext, the DataContext of the visual parent is used as the source of the binding.

Some controls automatically bind child controls' data contexts, for example ContentControl and ItemsControl.