Skip to content

WPF Data Templates

WPF Data Templates are used to describe how data objects shall be rendered on the UI. Because Data Templates build a central concept in WPF you will need to understand and make use of them.

A common use case of Data Templates is the definition of an item’s DataTemplate for an ItemsControl.

Example for a ListBox:

<ListBox ItemsSource="{Binding Students}"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding LastName}"/> <TextBlock Text=", "/> <TextBlock Text="{Binding FirstName}"/> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox>
Code language: HTML, XML (xml)

The DataTemplate definition describes how each Student object shall be rendered. In our example we display the LastName property followed by a ‘, ‘ and the FirstName property.

Copyright (c) by Thomas Kemp, 2021