February 27, 2021
Hot Topics:

Dynamically Building Windows Presentation Foundation DataTemplates

  • By Jeffrey Juday
  • Send Email »
  • More Articles »

Generating a DataTemplate

The following is the code generating the DataSet and DataTemplate.

ds = new DataSet();


ns = "xmlns=\"http://schemas.microsoft.com/winfx/2006/

   tempString.Append("<DataTemplate " + ns + ">");
   foreach (DataColumn col in ds.Tables[0].Columns)
      tempString.Append("<ColumnDefinition Name=\"" +
         col.ColumnName + "\" Width=\"100\" />");
   for (int n = 0; n < ds.Tables[0].Columns.Count; ++n)
      tempString.Append("<TextBlock Text=\"{Binding Path=[" +
         n.ToString() + "]}\" Grid.Column=\"" + n.ToString() +
      tempString.Append(" FontWeight=\"Bold\" />");

The line including the namespace is the most unusual part of the code. You may have noticed that this is the same namespace included when you create any XAML file in Visual Studio. Without the namespace, the code handling the XML data cannot validate the XAML.

A Grid View resource is also a good alternative to a DataTemplate to display data in a grid.

Following is a DataTemplate XAML snippet of what the Product table would look like with the first two fields in the select statement. Notice the Binding and Path statement in the TextBlock. In the example, the Path is assigned to the column in a particular Row in the DataSet.

<Grid xmlns="http://schemas.microsoft.com/winfx/2006/
      <ColumnDefinition Name="ProductID" Width="100" />
      <ColumnDefinition Name="Name" Width="100" />
   <TextBlock Text="{Binding Path=[0]}"
              Grid.Column="0" FontWeight="Bold" />
   <TextBlock Text="{Binding Path=[1]}" Grid.Column="1"
              FontWeight="Bold" />

Working with the Generated XAML

XAML is simply XML data. So, as you would guess, classes from the System.Xml namespace play a role. In this case, because a XamlReader Load function cannot accept a StringBuilder class, I created a XmlReader from the StringBuilder. The code appears below.

xml  = XmlReader.Create(sr);

temp = (DataTemplate)XamlReader.Load(xml);

The final steps assign the DataTemplate and data to the Listview control. Here is the code:

this.DataView.ItemTemplate = temp;
this.DataView.ItemsSource = _ds.Tables[0].Rows;

ItemTemplate and ItemsSource are both Dependency Properties. ItemsSource is the data to be rendered in the control. Collections assigned to ItemSource must have certain capabilites, but is not limited to the Rows on a DataTable. ItemTemplate is the DataTemplate to use to render the data in the control.

Inspecting a Row

In your solution, most likely you will want to inspect the data in a particular row. So, I added some code to copy the row data to a Textbox at the bottom of the screen when a user double-clicks a row in the ListView. The important thing to understand is that, underneath, a reference to the original data structure (DataRows) is maintained.


WPF leverages Windows; platform's rich visualization with a productive development experience reducing development time yet, at the same time, producing engaging user interfaces. DataTemplates are one of many mechanisms developers can employ to customize the look of their application. The .NET Framework's XML libraries and WPF classes make dynamically generating a DataTemplate easy.

Download the Code

You can download the code that accompanies this article here.


About the Author

Jeffrey Juday is a software developer specializing in enterprise application integration solutions utilizing BizTalk, SharePoint, WCF, WF, and SQL Server. Jeff has been developing software with Microsoft tools for more than 15 years in a variety of industries including: military, manufacturing, financial services, management consulting, and computer security. Jeff is a Microsoft BizTalk MVP. Jeff spends his spare time with his wife Sherrill and daughter Alexandra. You can reach Jeff at me@jeffjuday.com.

Page 2 of 2

This article was originally published on September 15, 2008

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date