How to Make Data More Presentable Using Data Template

Data Templates are used to present your data in a pleasing manner so that the user has a good visual experience with the application. With the data templates you can make different data look differently so that the user can identify it easily.

The Dependency controls can bind to data source. The data can be anything like person name, person object, and product object.

The control can display the data in any format. It can show the name or name and age or product name, product type etc.

This reporting format can be saved as template in the application resource and can be reused.

Advantages of Data Templates:

· Code reuse
· Easy maintenance of Data Template
· Flexible layout of controls

The sample below shows how to use data template to list the details of person in a list box control

<Window x:Class="SampleDataTemplate.Window1"
Title="Sample for DataTemplate" Height="250" Width="300"
<DataTemplate DataType="{x:Type local:Person}">
<Grid Margin="3">
<RowDefinition Height="auto"/>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="auto"/>

<StackPanel Grid.Column="0" Orientation="Horizontal">
<TextBlock FontSize="16" Foreground="Blue" Text="{Binding LastName}" />
<TextBlock FontSize="16" Foreground="Blue" xml:space="preserve">, </TextBlock>
<TextBlock FontSize="16" Foreground="Blue" Text="{Binding FirstName}" />
<TextBlock FontSize="16" Foreground="Blue" xml:space="preserve">, </TextBlock>

<TextBlock Grid.Column="1" FontSize="16" Foreground="Blue" Text="{Binding Address.Street}" />

<StackPanel Orientation="Horizontal" Grid.Column="2">
<TextBlock FontSize="16" Foreground="Blue" xml:space="preserve">, </TextBlock>
<TextBlock FontSize="16" Foreground="Blue" Text="{Binding Address.City}" />
<TextBlock FontSize="16" Foreground="Blue" xml:space="preserve">, </TextBlock>
<TextBlock FontSize="16" Foreground="Blue" Text="{Binding Address.State}" />

<ListBox Name="personList" SelectionChanged="SelectedPersonChanged" />


The key points to note in the above example are:

The grid has a resource DataTemplate for type person as

DataTemplate DataType="{x:Type local:Person}"

The Person is a class with properties of person defined. The data template defines controls for displaying attributes. They also specify the data binding.

The list box is also defined after the resource as,

<ListBox Name="personList" />

In code behind file the itemsource property is set with collection of person object.

personList.ItemsSource = collection;

Once the ItemSource property is set, the personlist automatically choose the DataTemplate from the window resource.

The code behind file for the above example is:

namespace SampleDataTemplate
public partial class Window1 : Window
public Window1()

protected virtual void WindowLoaded(object sender, EventArgs e)
ObservableCollection<Person> collection = new ObservableCollection<Person>();

for (int i = 0; i < 10; i++)
Person p = new Person();

p.FirstName = "Ramesh" + i;
p.LastName = "Gupta";

Address a = new Address();

a.Street = "Dr Street";
a.City = "New Delhi";
a.State = "Delhi";

p.Address = a;


personList.ItemsSource = collection;


Thus you can use data templates to present data in a more pleasing manner so that it is easy for the user to read it. Without data templates the data that is displayed will not be that much readable for the end user.


Add to My Yahoo!

FREE Subscription

Subscribe to our mailing list and receive new articles
through email. Keep yourself updated with latest
developments in the industry.


Note : We never rent, trade, or sell my email lists to
anyone. We assure that your privacy is respected
and protected.

Visit .NET Programming Tutorial Homepage


Recommended Resource

| Privacy Policy for | Disclosure | Contact |

Copyright - © 2004 - 2017 - All Rights Reserved.