Skip to main content
Version: 11.0.x

Data Templates Collection

Every control in Avalonia UI has a DataTemplates collection where you can place any number of data template definitions. You can then choose the template to use for display by class type.

When a control does not have a data template set directly in its ContentTemplate property (as on the previous page); then it will choose a template from in its DataTemplates collection that matches the class of the object being displayed. This applies to a window.

Data templates are matched by type: a match occurs when the class of the object being displayed is the same as the fully-qualified class name specified in the DataType property of a template.

So you can modify the previous sample to use the DataTemplates collection, as follows:

<Window xmlns=""
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
<DataTemplate DataType="{x:Type local:Student}">
<Grid ColumnDefinitions="Auto,Auto" RowDefinitions="Auto,Auto">
<TextBlock Grid.Row="0" Grid.Column="0">First Name:</TextBlock>
<TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding FirstName}"/>
<TextBlock Grid.Row="1" Grid.Column="0">Last Name:</TextBlock>
<TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding LastName}"/>

<local:Student FirstName="Jane" LastName="Deer"/>

This results in exactly the same display as on the previous page: