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.
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".
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.
<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="<TextBox Text="{Binding Name}" />" />
<TextBlock Classes="code" Text="<TextBlock Text="{Binding Greeting}" />" />
<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) => Greeting = $"Hello, {value}";" />
</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
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.Textfrom 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.Textis 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
- Add an
Agefield and anIsAdultcomputed property; bind a caption to it. - Delete
x:DataTypefrom the root and rebuild. Read the error: that is the compiler doing the work a reflection binding would have skipped. - Add a second
TextBoxbound to the sameName. Both update each other; no event handler was written.
Files this lesson draws from
src/CourseApp/Lessons/02_Xaml/BindingsView.axamlsrc/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.