avalonia-data-templates
Use when defining DataTemplate, ItemTemplate, ContentTemplate, selecting templates by data type, using TreeDataTemplate for hierarchical data, implementing IDataTemplate selectors, or using FuncDataTemplate in Avalonia. Covers template scoping, app-level templates, and compiled bindings in templates (default in 12.0+, so `x:DataType` is required; `{ReflectionBinding}` as opt-out), including TableView/DataGrid column templates.
linuxdevel · 已有静态检查记录
安装与使用
运行命令前先阅读来源与依赖说明。复制命令不会自动安装或执行。
npx skills add linuxdevel/Avalonia-skills --skill avalonia-data-templates技能说明
name: avalonia-data-templates
description: Use when defining DataTemplate, ItemTemplate, ContentTemplate, selecting templates by data type, using TreeDataTemplate for hierarchical data, implementing IDataTemplate selectors, or using FuncDataTemplate in Avalonia. Covers template scoping, app-level templates, and compiled bindings in templates (default in 12.0+, so x:DataType is required; {ReflectionBinding} as opt-out), including TableView/DataGrid column templates.
Avalonia Data Templates
Overview
DataTemplates define how a data object is rendered as UI. They apply to:
ItemsControland subclasses (ListBox,TreeView) viaItemTemplateTableViewColumn.CellTemplate(12.1+) and DataGrid template columns (see Column Templates below)ContentControland subclasses viaContentTemplate- Any
Contentproperty — Avalonia walks up the template tree to find a matchingDataTemplate
Template matching is by the data object's runtime type. First matching template in scope wins.
Inline DataTemplate
<ListBox ItemsSource="{Binding People}">
<ListBox.ItemTemplate>
<DataTemplate x:DataType="vm:PersonViewModel">
<StackPanel Orientation="Horizontal" Spacing="8">
<TextBlock Text="{Binding FirstName}"/>
<TextBlock Text="{Binding LastName}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
x:DataType enables compiled bindings inside the template.
12.0+: compiled bindings are the default (
AvaloniaUseCompiledBindingsByDefaultis on in new templates), so every{Binding}needs a knownx:DataTypeor the build fails (AVLN2000). For genuinely dynamic data use{ReflectionBinding Path}. 11.x: compiled bindings are opt-in (x:CompileBindings="True"/ the MSBuild property); without it{Binding}is reflection-based. Seeavalonia-versions.
ContentControl with DataTemplate
<ContentControl Content="{Binding CurrentPage}">
<ContentControl.ContentTemplate>
<DataTemplate x:DataType="vm:HomeViewModel">
<views:HomeView/>
</DataTemplate>
</ContentControl.ContentTemplate>
</ContentControl>
For multi-page navigation, app-level templates (see below) are more practical than per-ContentControl templates.
App-Level Templates (Global Auto-Selection)
In App.axaml, templates registered here apply everywhere in the application. Avalonia automatically uses the matching template when a ViewModel type appears as Content or as an item.
<Application.DataTemplates>
<DataTemplate x:DataType="vm:HomeViewModel">
<views:HomeView/>
</DataTemplate>
<DataTemplate x:DataType="vm:SettingsViewModel">
<views:SettingsView/>
</DataTemplate>
<DataTemplate x:DataType="vm:PersonViewModel">
<StackPanel>
<TextBlock Text="{Binding FullName}" FontWeight="Bold"/>
<TextBlock Text="{Binding Email}"/>
</StackPanel>
</DataTemplate>
</Application.DataTemplates>
With this setup, a ContentControl binding to a ViewModel just works:
<!-- Automatically renders the right view -->
<ContentControl Content="{Binding ActivePage}"/>
Template Scope Resolution Order
Avalonia searches for a matching DataTemplate in this order:
ContentControl.ContentTemplateorItemsControl.ItemTemplate(explicit, highest priority)Control.DataTemplateson the control itselfDataTemplatesin parent controls, walking up the logical treeApplication.DataTemplates(global fallback)
First match wins. Order within a collection matters — put more specific types before base types.
DataTemplate Type Selector (IDataTemplate)
For conditional template selection based on runtime type or property values, implement IDataTemplate:
public class AnimalTemplateSelector : IDataTemplate
{
// Set these from XAML
public IDataTemplate? DogTemplate { get; set; }
public IDataTemplate? CatTemplate { get; set; }
public Control? Build(object? param) => param switch
{
Dog d => DogTemplate?.Build(d),
Cat c => CatTemplate?.Build(c),
_ => new TextBlock { Text = $"Unknown: {param}" }
};
public bool Match(object? data) => data is Dog or Cat;
}
<Window.Resources>
<local:AnimalTemplateSelector x:Key="AnimalSelector">
<local:AnimalTemplateSelector.DogTemplate>
<DataTemplate x:DataType="vm:Dog">
<TextBlock Text="{Binding Breed}"/>
</DataTemplate>
</local:AnimalTemplateSelector.DogTemplate>
<local:AnimalTemplateSelector.CatTemplate>
<DataTemplate x:DataType="vm:Cat">
<TextBlock Text="{Binding Color}"/>
</DataTemplate>
</local:AnimalTemplateSelector.CatTemplate>
</local:AnimalTemplateSelector>
</Window.Resources>
<ItemsControl ItemsSource="{Binding Animals}"
ItemTemplate="{StaticResource AnimalSelector}"/>
FuncDataTemplate (Code-Only)
Useful in code-behind, custom controls, or when XAML is impractical:
// Simple
var template = new FuncDataTemplate<PersonViewModel>((person, _) =>
new TextBlock { Text = person.FullName });
// With bindings
var template = new FuncDataTemplate<PersonViewModel>((person, scope) =>
{
var tb = new TextBlock();
// 12.0+: compiled binding from a lambda (no reflection)
tb.Bind(TextBlock.TextProperty,
CompiledBinding.Create((PersonViewModel p) => p.FullName));
// 11.x idiom / reflection fallback: new Binding(nameof(PersonViewModel.FullName))
// 12: new ReflectionBinding(nameof(PersonViewModel.FullName))
return tb;
});
myListBox.ItemTemplate = template;
Column Templates (TableView 12.1+, DataGrid)
<!-- TableView (12.1+) -->
<TableView ItemsSource="{Binding People}">
<TableView.Columns>
<TableViewColumn Header="N
阅读完整来源 ↗