ITemplate)FuncTemplate)IControlTemplateIRecyclingDataTemplate)FuncTreeDataTemplate and TreeDataTemplate)TemplateContent)TemplateExtensions)Primary APIs:
ITemplate<TControl>ITemplate<TParam, TControl>FuncTemplate<TControl>FuncTemplate<TParam, TControl>FuncTemplateNameScopeExtensionsRegisterInNameScope<T>(...)IControlTemplateIRecyclingDataTemplateFuncTreeDataTemplateFuncTreeDataTemplate<T>TreeDataTemplateTemplateContentTemplateExtensionsTemplateExtensions.GetTemplateChildren(...)Frequently referenced signatures:
FuncTemplate(Func<TControl> func)FuncTemplate(Func<TParam, INameScope, TControl> func)FuncTreeDataTemplate(Type type, Func<object?, INameScope, Control> build, Func<object?, IEnumerable> itemsSelector)FuncTreeDataTemplate(Func<object?, bool> match, Func<object?, INameScope, Control?> build, Func<object?, IEnumerable> itemsSelector)FuncTreeDataTemplate(Func<T, INameScope, Control> build, Func<T, IEnumerable> itemsSelector)FuncTreeDataTemplate(Func<T, bool> match, Func<T, INameScope, Control> build, Func<T, IEnumerable> itemsSelector)FuncTreeDataTemplate.ItemsSelector(object item)TreeDataTemplate.ItemsSelector(object item)TemplateContent.Load(object? templateContent)TemplateContent.Load<T>(object? templateContent)Reference source files:
src/Avalonia.Controls/Templates/ITemplate\1.cs`src/Avalonia.Controls/Templates/ITemplate\2.cs`src/Avalonia.Controls/Templates/FuncTemplate\1.cs`src/Avalonia.Controls/Templates/FuncTemplate\2.cs`src/Avalonia.Controls/Templates/FuncTemplateNameScopeExtensions.cssrc/Avalonia.Controls/Templates/IControlTemplate.cssrc/Avalonia.Controls/Templates/IRecyclingDataTemplate.cssrc/Avalonia.Controls/Templates/FuncTreeDataTemplate.cssrc/Avalonia.Controls/Templates/FuncTreeDataTemplate\1.cs`src/Avalonia.Controls/Templates/TemplateExtensions.cssrc/Markup/Avalonia.Markup.Xaml/Templates/TemplateContent.cssrc/Markup/Avalonia.Markup.Xaml/Templates/TreeDataTemplate.csITemplate)Core contracts:
ITemplate<TControl> builds a control with no explicit input parameter.ITemplate<TParam, TControl> builds a control from an explicit parameter.These generic interfaces are the shared base for control templates, data-template helpers, and function-based builders.
Use:
ITemplate<TControl> for template instances with implicit context.ITemplate<TParam, TControl> when the caller passes explicit state/input.FuncTemplate)FuncTemplate wrappers let you build templates from strongly typed delegates.
Types:
FuncTemplate<TControl> : ITemplate<TControl>FuncTemplate<TParam, TControl> : ITemplate<TParam, TControl>Constructors:
FuncTemplate(Func<TControl> func)FuncTemplate(Func<TParam, INameScope, TControl> func)Example:
using Avalonia.Controls;
using Avalonia.Controls.Templates;
var noArg = new FuncTemplate<TextBlock>(() => new TextBlock { Text = "Hello" });
TextBlock built = noArg.Build();
var withParam = new FuncTemplate<string, TextBlock>((value, scope) =>
{
var text = new TextBlock { Name = "PART_Label", Text = value };
return text.RegisterInNameScope(scope);
});
TextBlock builtWithParam = withParam.Build("Template value");
FuncTemplateNameScopeExtensions provides helper registration for function-template output.
API:
FuncTemplateNameScopeExtensionsRegisterInNameScope<T>(this T control, INameScope scope) where T : StyledElementUse this when function templates create named elements that must be discoverable from template name scope.
IControlTemplateIControlTemplate is the typed contract for TemplatedControl template building.
API:
IControlTemplate : ITemplate<TemplatedControl, TemplateResult<Control>?>Practical guidance:
ControlTemplate in XAML for normal app authoring,IRecyclingDataTemplate)IRecyclingDataTemplate extends IDataTemplate with recycle-aware build:
Control? Build(object? data, Control? existing)Use when item controls are frequently re-bound and you can reliably reset reused state.
If recycling is enabled, ensure state cleanup is deterministic:
FuncTreeDataTemplate and TreeDataTemplate)Tree-template APIs:
FuncTreeDataTemplateFuncTreeDataTemplate<T>TreeDataTemplateFunction-based hierarchical templates:
FuncTreeDataTemplate(Type type, Func<object?, INameScope, Control> build, Func<object?, IEnumerable> itemsSelector)FuncTreeDataTemplate(Func<object?, bool> match, Func<object?, INameScope, Control?> build, Func<object?, IEnumerable> itemsSelector)FuncTreeDataTemplate(Func<T, INameScope, Control> build, Func<T, IEnumerable> itemsSelector)FuncTreeDataTemplate(Func<T, bool> match, Func<T, INameScope, Control> build, Func<T, IEnumerable> itemsSelector)Example:
using Avalonia.Controls;
using Avalonia.Controls.Templates;
var treeTemplate = new FuncTreeDataTemplate<MyNode>(
build: (node, scope) => new TextBlock { Name = "PART_Title", Text = node.Title }.RegisterInNameScope(scope),
itemsSelector: node => node.Children);
XAML tree template path:
<TreeDataTemplate xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="using:MyApp.ViewModels"
x:DataType="vm:NodeViewModel"
ItemsSource="{CompiledBinding Children}">
<TextBlock Text="{CompiledBinding Title}" />
</TreeDataTemplate>
API-index compatibility note:
references/api-index-generated.md includes FuncTreeDataTemplate.ItemsSelector(object item) and TreeDataTemplate.ItemsSelector(object item) signatures.11.3.12 usage, author against FuncTreeDataTemplate constructor selector functions and TreeDataTemplate.ItemsSource.TemplateContent)TemplateContent is the loader utility used by markup template wrappers.
APIs:
TemplateContentTemplateContent.Load(object? templateContent)TemplateContent.Load<T>(object? templateContent)Use this when handling deferred template content in infrastructure code; app-level code normally consumes higher-level ControlTemplate, DataTemplate, and TreeDataTemplate.
TemplateExtensions)TemplateExtensions.GetTemplateChildren(this TemplatedControl control) traverses controls created from the control’s applied template.
Use cases:
OnApplyTemplate,Example:
using Avalonia.Controls.Primitives;
using Avalonia.Controls.Templates;
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{
base.OnApplyTemplate(e);
foreach (var child in this.GetTemplateChildren())
{
// Inspect template-built controls tied to this templated parent.
}
}
FuncTemplate/function-template APIs for dynamic or infrastructure scenarios.IRecyclingDataTemplate opt-in and explicit.
RegisterInNameScope(...).TemplateExtensions.GetTemplateChildren(...) for diagnostics and verification, not routine business logic.INameScope (RegisterInNameScope(...) in function templates).IRecyclingDataTemplate.Build(data, existing) must fully reset view state before reuse.FuncTreeDataTemplate selector or TreeDataTemplate.ItemsSource) returns expected enumerable.GetTemplateChildren(...) is scoped to templated-parent ownership, not all visual descendants.TemplateContent.Load(...) expects.