Skip to content

Latest commit

 

History

History
22 lines (16 loc) · 654 Bytes

creating-data-templates-in-code.md

File metadata and controls

22 lines (16 loc) · 654 Bytes

Creating Data Templates in Code

Avalonia also supports creating data templates in code with the FuncDataTemplate<T> class.

At its simplest you can create a data template by passing a lambda which accepts an instance to the FuncDataTemplate<T> constructor:

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

Is equivalent to:

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