avalonia docs

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.

When using the MVVM pattern, the data context will usually be an instance of a view model.

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

private static void AppMain(Application app, string[] args)
    var window = new MainWindow
        DataContext = new MainWindowViewModel(),


This means 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:

    <Button Content="{Binding ButtonCaption}"/>

Will bind the Button's Content to Window.DataContext.ButtonCaption.

Binding DataContext

When binding DataContext, the DataContext of the parent control is used as the source of the binding:

    <!-- Will bind `DataContext` to `Window.DataContext.Content -->
    <StackPanel DataContext="{Binding Content}"/>

Controls that display content based on a data template will automatically set the DataContext for the controls in the template. For example ContentControl:

    <ContentControl DataContext="{Binding Content}">
                <!-- Will bind `Text` to `Window.DataContext.Content.Header -->
                <TextBlock Text="{Binding Header}"/>