Compiled Bindings
Bindings defined in the XAML are using reflection in order to find and access the requested property in your ViewModel. In Avalonia you can also use compiled bindings, which has some benefits:
  • If you use compiled bindings and the property you bind to is not found, you will get a compile-time error. Hence you get a much better debugging experience.
  • Reflection is known to be slow (see this article on codeproject.com). Using compiled bindings can therefore improve the performance of your application.

Enable and disable compiled bindings

Compiled bindings are not enabled by default. To enable compiled bindings, you will need to define the DataType of the object you want to bind to first. In DataTemplates there is a property DataType, for all other elements you can set it via x:DataType. Most likely you will set x:DataType in your root node, for example in a Window or an UserControl. From Avalonia version 0.10.12 onward you can also specify the DataType in the Binding directly.
You can now enable or disable compiled bindings by setting x:CompileBindings="[True|False]". All child nodes will inherit this property, so you can enable it in your root node and disable it for a specific child, if needed.
1
<!-- Set DataType and enable compiled bindings -->
2
<UserControl xmlns="https://github.com/avaloniaui"
3
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4
xmlns:vm="using:MyApp.ViewModels"
5
x:DataType="vm:MyViewModel"
6
x:CompileBindings="True">
7
<StackPanel>
8
<TextBlock Text="Last name:" />
9
<TextBox Text="{Binding LastName}" />
10
<TextBlock Text="Given name:" />
11
<TextBox Text="{Binding GivenName}" />
12
<TextBlock Text="E-Mail:" />
13
<!-- Set DataType inside the Binding-markup -->
14
<TextBox Text="{Binding MailAddress, DataType={x:Type vm:MyViewModel}}" />
15
16
<!-- We cannot use complied bindings to bind to methods, so we opt them out for the button -->
17
<Button x:CompiledBindings="False"
18
Content="Send an E-Mail"
19
Command="{Binding SendEmailCommand}" />
20
</StackPanel>
21
</UserControl>
Copied!

CompiledBinding-Markup

If you don't want to enable compiled bindings for all child nodes, you can also use the CompiledBinding-markup. You still need to define the DataType, but you can omit x:CompileBindings="True".
1
<!-- Set DataType -->
2
<UserControl xmlns="https://github.com/avaloniaui"
3
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4
xmlns:vm="using:MyApp.ViewModels"
5
x:DataType="vm:MyViewModel">
6
<StackPanel>
7
<TextBlock Text="Last name:" />
8
<!-- use CompiledBinging markup for your binding -->
9
<TextBox Text="{CompiledBinding LastName}" />
10
<TextBlock Text="Given name:" />
11
<TextBox Text="{CompiledBinding GivenName}" />
12
<TextBlock Text="E-Mail:" />
13
<TextBox Text="{CompiledBinding MailAddress}" />
14
15
<!-- We cannot use complied bindings to bind to methods, so we use the normal Binding -->
16
<Button x:CompiledBindings="False"
17
Content="Send an E-Mail"
18
Command="{Binding SendEmailCommand}" />
19
</StackPanel>
20
</UserControl>
Copied!

ReflectionBinding-Markup

If you have compiled bindings enabled in the root node (via x:CompileBindings="True") and you either don't want to use complied binding at a certain position or you hit one of the known limitations, you can use the ReflectionBinding-markup.
1
<!-- Set DataType -->
2
<UserControl xmlns="https://github.com/avaloniaui"
3
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4
xmlns:vm="using:MyApp.ViewModels"
5
x:DataType="vm:MyViewModel">
6
<StackPanel>
7
<TextBlock Text="Last name:" />
8
<!-- use CompiledBinging markup for your binding -->
9
<TextBox Text="{Binding LastName}" />
10
<TextBlock Text="Given name:" />
11
<TextBox Text="{Binding GivenName}" />
12
<TextBlock Text="E-Mail:" />
13
<TextBox Text="{Binding MailAddress}" />
14
15
<!-- We cannot use complied bindings to bind to methods, so we use ReflectionBinding instead -->
16
<Button x:CompiledBindings="False"
17
Content="Send an E-Mail"
18
Command="{ReflectionBinding SendEmailCommand}" />
19
</StackPanel>
20
</UserControl>
Copied!

Known limitations

Compiled bindings have some known limitations:
  • Compiled bindings cannot be used to bind to named elements
  • Compiled bindings cannot be used in Styles with RelativeSource set to TemplatedParent (e.g.: {Binding Width, RelativeSource={RelativeSource TemplatedParent}})
If you hit one of those limitations you should disable compiled bindings for the failing XAML-node or use ReflectionBinding instead.