Last updated 1 year ago
Avalonia也支持使用类在代码中创建数据模板。
简单来说,你可以通过将一个接受实例和namescope的lambda表达式传递给FuncDataTemplate<T>构造函数来创建一个数据模板:
namescope
FuncDataTemplate<T>
var template = new FuncDataTemplate<Student>((value, namescope) => new TextBlock { [!TextBlock.TextProperty] = new Binding("FirstName"), });
它等价于以下XAML代码:
<DataTemplate DataType="{x:Type local:Student}"> <TextBlock Text="{Binding FirstName}"/> </DataTemplate>