Skip to main content
Version: 11.0.x

Creating in Code

Avalonia UI supports creating a data template in code. You can do this by using the FuncDataTemplate<T> class that supports the IDataTemplate interface.

At its simplest you can create a data template by passing a lambda function that creates a control to the FuncDataTemplate<T> constructor, like this:

var template = new FuncDataTemplate<Student>((value, namescope) =>
new TextBlock
{
[!TextBlock.TextProperty] = new Binding("FirstName"),
});

Which is equivalent to the XAML:

<DataTemplate DataType="{x:Type local:Student}">
<TextBlock Text="{Binding FirstName}"/>
</DataTemplate>

More Examples

info

Have a look at some more advanced uses of the FuncDataTemplate<T>class in the Avalonia UI sample project here.