Install
$ agentstack add skill-linuxdevel-avalonia-skills-avalonia-data-templates ✓ scanned · ✓ verified — works with Claude Code, Cursor, and more.
Security review
✓ PassedNo issues found. Passed automated security review. · v0.1.0 How review works →
- ✓ Prompt-injection patterns
- ✓ Secret / credential exfiltration
- ✓ Dangerous shell & filesystem operations
- ✓ Untrusted network calls
- ✓ Known-malicious package signatures
What it can access
- ✓ Network access No
- ✓ Filesystem access No
- ✓ Shell / process execution No
- ✓ Environment & secrets No
- ✓ Dynamic code execution No
From automated source analysis of v0.1.0. “Used” means the capability is present in the source — more access means more to trust, not that it’s unsafe.
About
Avalonia Data Templates
Overview
DataTemplates define how a data object is rendered as UI. They apply to:
ItemsControland subclasses (ListBox,TreeView,DataGrid) viaItemTemplateContentControland 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
x:DataType enables compiled bindings inside the template. Every DataTemplate that uses compiled bindings must declare it.
ContentControl with DataTemplate
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.
With this setup, a ContentControl binding to a ViewModel just works:
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;
}
FuncDataTemplate (Code-Only)
Useful in code-behind, custom controls, or when XAML is impractical:
// Simple
var template = new FuncDataTemplate((person, _) =>
new TextBlock { Text = person.FullName });
// With bindings
var template = new FuncDataTemplate((person, scope) =>
{
var tb = new TextBlock();
tb.Bind(TextBlock.TextProperty, new Binding(nameof(PersonViewModel.FullName)));
return tb;
});
myListBox.ItemTemplate = template;
Reusable Templates as Resources
Define once, reference many times:
TreeDataTemplate (Hierarchical / TreeView)
TreeDataTemplate extends DataTemplate with an ItemsSource binding for child nodes:
ViewModel:
public class TreeNode
{
public string Name { get; set; } = "";
public IImage? Icon { get; set; }
public ObservableCollection Children { get; } = new();
}
Mixed types in a tree (files and folders):
public class AnimalTreeSelector : IDataTemplate
{
public Control? Build(object? param) => param switch
{
FolderNode => /* build folder template */,
FileNode => /* build file template */,
_ => null
};
public bool Match(object? data) => data is FolderNode or FileNode;
}
ItemsControl Panel Customization
Change the layout panel used to arrange items:
Design-Time Data in Templates
Common Mistakes
- Missing
x:DataTypeon DataTemplate — compiled bindings inside the template have no type context and fail to build. Every DataTemplate using{Binding}needsx:DataType. - Wrong resource scope — a DataTemplate in
Window.Resourcesis not visible to templates inside a nestedUserControl. EachUserControlhas its own resource scope. Put shared templates inApplication.ResourcesorApplication.DataTemplates. - Using
DataTemplateinstead ofTreeDataTemplatefor tree data —DataTemplaterenders the node content but does not provide anItemsSourcebinding for children;TreeViewwill not recurse into children. - Matching on interface type — Avalonia's automatic type-based template selection matches on the concrete runtime type, not interfaces.
IDataTemplate.Match()must explicitly check for the concrete type, or register templates against the concrete type. - Multiple templates for same type in same scope — if two
DataTemplates inApplication.DataTemplateshavex:DataType="vm:PersonViewModel", the first one always wins. Remove duplicates or use keyed resources for multiple representations. - Circular template reference — a
DataTemplatethat creates aContentControlwhoseContentbinds back to the same ViewModel type causes infinite recursion. Use a keyed template or break the cycle with a wrapper ViewModel. - Setting both
ItemTemplateand using auto-selection — an explicitItemTemplatealways takes precedence overApplication.DataTemplates. RemoveItemTemplateto rely on global auto-selection.
Source & license
This open-source skill is cataloged on AgentStack and links to its original source — we do not rehost the code.
- Author: linuxdevel
- Source: linuxdevel/Avalonia-skills
- License: MIT
Install and usage instructions live in the source repository linked above.
Reviews
No reviews yet — be the first.
Write a review
Versions
- v0.1.0 Imported from the upstream source.