DataTemplate In WPF

Data template is a bit of XAML that describes how bound data is displayed. A data template can contain elements that are each bound to a data property along with additional markup that describes layout, color and other appearance. DataTemplate is, basically, used to specify the appearance of data displayed by a control not the appearance of the control itself.

Therefore, DataTemplate can be applied to ContentControls or ItemsControl. The name of the property to which you can assign a DataTemplate depends whether the control is a content control or an ItemsControl. So what are ContentControls and ItemsControl?

The controls that can contain a single item (single logical child of type Object) are called “content controls”. These controls derive from the ContentControl base class. Controls that can contain a collection of items (many logical children of type Object) are called “items controls”.

Let’s understand with an example:

We want to fill a Listbox with employee details such as employee name, employee number, address and telephone number.

empinfo

But, Listbox can be bounded only with one data property of the employee class (shown below). If we bind Listbox itemsource property to employee array, binding calls ToString() method of the class and populates the listbox with the return value.

data
If ToString () method is overridden and it returns the desired value, the same can be displayed in the listbox.

  1. public override string  ToString()  
  2.         {  
  3.             return this.Name;  
  4.         }  
Data display:

display

In order to achieve what we started with, we need to do more. Therefore, we need to change the datatemplate of the listbox.

listbox

Datatemplate should be changed in such a way that details of an employee are displayed as shown above.
  1. <DataTemplate x:Key="myTemplate">  
  2.             <StackPanel>  
  3.                 <Label Background="Purple" Foreground="White" BorderBrush="Red" BorderThickness="4">  
  4.                     <Label.Content>  
  5.                         <WrapPanel HorizontalAlignment="Stretch">  
  6.                             <TextBlock>Emplyoee Name:</TextBlock>  
  7.                             <TextBlock Text="{Binding Name}" />  
  8.                         </WrapPanel>  
  9.                     </Label.Content>  
  10.                 </Label>  
  11.                 <Label  BorderBrush="Black" HorizontalAlignment="Stretch" Background="Yellow" BorderThickness="3" Foreground="Blue">  
  12.                     <Label.Content>  
  13.                         <StackPanel>  
  14.                             <WrapPanel>  
  15.                                 <TextBlock> Employee Nr.:</TextBlock>  
  16.                                 <TextBlock Text="{Binding EmployeeNr}"/>  
  17.                             </WrapPanel>  
  18.                             <WrapPanel>  
  19.                                 <TextBlock> Department:</TextBlock>  
  20.                                 <TextBlock Text="{Binding Dept}"/>  
  21.                             </WrapPanel>  
  22.                             <WrapPanel>  
  23.                                 <TextBlock> Telephone:</TextBlock>  
  24.                                 <TextBlock Text="{Binding TelephoneNr}"/>  
  25.                             </WrapPanel>  
  26.                         </StackPanel>  
  27.                     </Label.Content>  
  28.                 </Label>  
  29.             </StackPanel>  
  30.         </DataTemplate>  
It is recommended that data templates are defined in a resource collection and referenced in your element rather than defining them inline. All that is required to reuse a data template in this manner is to define the template in a resource collection and set a Key property for the template, as shown here:
  1. <Window.Resources>   
  2.         <DataTemplate x:Key="myTemplate">  
  3.   
  4. <!--- details here - - >  
  5.   
  6. </DataTemplate>  
  7.     </Window.Resources>  
Then you can set the template by referring to the resource, as shown in bold here:
  1. <ListBox ItemTemplate="{StaticResource myTemplate}" Name="empList" />  
When binding a property or list directly to a control, you are limited to binding a single property. With data templates, however, you can bind more than one property in each item, thereby displaying multiple bits of related data together.
 
Read more articles on WPF:

Up Next
    Ebook Download
    View all
    Learn
    View all