Avalonia Field Guide
About

Module 1 · Lesson 02 of 16

Bindings the compiler can see

XAML maps elements to types and attributes to properties. In Avalonia 12, bindings compile by default — a typo becomes a build error.

25 minHeadless-tested

XAML is not a language. It is a serialization of constructor calls and property assignments. <TextBox Text="{Binding Name}" /> means "create a TextBox, then set its Text property to a binding on Name".

Screenshot of the xaml lesson sample
The lesson's sample, rendered headlessly at 1280×800. Light and dark are both captured.

The one attribute that matters

<UserControl x:DataType="local:BindingsViewModel">

x:DataType tells the compiler what the data context will be. With it, {Binding Name} becomes a direct call to the Name property: no reflection, no string lookup at runtime, and — the useful part — a compile error if the property does not exist.

Avalonia 12 enables compiled bindings by default. Tutorials written for 11.x often rely on reflection bindings, and their {Binding} paths silently resolve at runtime. If you copy one of those into a 12 project, you will get build errors instead. That is the feature working.

src/CourseApp/Lessons/02_Xaml/BindingsView.axaml
<UserControl xmlns="https://github.com/avaloniaui"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:shell="using:CourseApp.Shell"
             xmlns:local="using:CourseApp.Lessons.Xaml"
             x:Class="CourseApp.Lessons.Xaml.BindingsView"
             x:DataType="local:BindingsViewModel">

    <ScrollViewer>
        <StackPanel Classes="page" Spacing="28">
            <shell:PageHeader
                Kicker="Lesson 02 · XAML"
                Title="Bindings the compiler can see"
                Blurb="Avalonia 12 compiles bindings by default. x:DataType turns every typo in a property path into a build error instead of a silent blank control." />

            <Grid ColumnDefinitions="320,*">
                <Border Classes="card">
                    <StackPanel Spacing="12">
                        <TextBlock Text="DISPLAY NAME" Classes="kicker" Foreground="{DynamicResource InkMuted}" />
                        <TextBox Text="{Binding Name}" PlaceholderText="Ada Lovelace" />
                        <TextBlock Text="{Binding Greeting}" FontSize="19" FontWeight="SemiBold" />
                        <TextBlock Classes="caption" Text="Type above. Nothing is wired by hand." />
                    </StackPanel>
                </Border>

                <Border Grid.Column="1" Classes="card" Margin="16,0,0,0" Background="{DynamicResource SurfaceMuted}">
                    <StackPanel Spacing="10">
                        <TextBlock Classes="kicker" Text="What the XAML declares" />
                        <TextBlock Classes="code" Text="&lt;TextBox Text=&quot;{Binding Name}&quot; /&gt;" />
                        <TextBlock Classes="code" Text="&lt;TextBlock Text=&quot;{Binding Greeting}&quot; /&gt;" />
                        <Border Classes="rule" Margin="0,4" />
                        <TextBlock Classes="caption" TextWrapping="Wrap"
                                   Text="With x:DataType set, the generated code calls the real properties. Rename Name and this page stops compiling." />
                    </StackPanel>
                </Border>
            </Grid>

            <StackPanel Spacing="6" MaxWidth="760" HorizontalAlignment="Left">
                <TextBlock Classes="caption" TextWrapping="Wrap"
                           Text="The view model raises PropertyChanged when Name changes; the greeting is recomputed in the same notification. No control is touched from code." />
                <TextBlock Classes="code" Text="partial void OnNameChanged(string value) =&gt; Greeting = $&quot;Hello, {value}&quot;;" />
            </StackPanel>
        </StackPanel>
    </ScrollViewer>
</UserControl>

Notice what is absent: no x:Name, no Click handler, no code that reaches into a control. The view declares structure and the direction of data; the view model owns the state.

The other half

src/CourseApp/Lessons/02_Xaml/BindingsViewModel.cs
using CommunityToolkit.Mvvm.ComponentModel;

namespace CourseApp.Lessons.Xaml;

public partial class BindingsViewModel : ObservableObject
{
    [ObservableProperty]
    private string _name = "Ada Lovelace";

    [ObservableProperty]
    private string _greeting = "Hello, Ada Lovelace";

    partial void OnNameChanged(string value) =>
        Greeting = string.IsNullOrWhiteSpace(value) ? "Hello, stranger" : $"Hello, {value}";
}

[ObservableProperty] turns the field into a property with a setter that raises PropertyChanged. OnNameChanged is a generated partial hook: the toolkit calls it after the value changes, which is why Greeting is recomputed exactly once per edit.

Why not just set TextBlock.Text from a click handler? Because then the state lives in the control tree. The moment a second view, a test, or a keyboard shortcut needs the same state, you are reading pixels instead of a model.

Values that are not properties

  • {Binding Name} — one-way by default for most properties; TextBox.Text is two-way.
  • {Binding Name, Mode=OneWay} — explicit, and worth writing when it matters.
  • {Binding Count, StringFormat='{}{0} items'} — the {} prefix escapes a format string that starts with a brace.
  • $parent[Window].Title — ancestor binding when a template's data context is an item rather than the page.

Try it

  1. Add an Age field and an IsAdult computed property; bind a caption to it.
  2. Delete x:DataType from the root and rebuild. Read the error: that is the compiler doing the work a reflection binding would have skipped.
  3. Add a second TextBox bound to the same Name. Both update each other; no event handler was written.

Next: star sizing, and nothing else.

Files this lesson draws from

  • src/CourseApp/Lessons/02_Xaml/BindingsView.axaml
  • src/CourseApp/Lessons/02_Xaml/BindingsViewModel.cs

VerifiedThe verifier asserts that assigning Name recomputes Greeting, so the page depends on a live binding rather than a static string.

Back to top