Export (0) Print
Expand All
0 out of 1 rated this helpful - Rate this topic

ItemsControl.ItemTemplate Property

Gets or set the DataTemplate used to display each item. This is a dependency property.

Namespace: System.Windows.Controls
Assembly: PresentationFramework (in presentationframework.dll)
XML Namespace:  http://schemas.microsoft.com/winfx/2006/xaml/presentation

[BindableAttribute(true)] 
public DataTemplate ItemTemplate { get; set; }
/** @property */
public DataTemplate get_ItemTemplate ()

/** @property */
public void set_ItemTemplate (DataTemplate value)

public function get ItemTemplate () : DataTemplate

public function set ItemTemplate (value : DataTemplate)

<object>
  <object.ItemTemplate>
    <DataTemplate .../>
  </object.ItemTemplate>
</object>
<object ItemTemplate=" ResourceExtensionTemplateResourceKey"/>
 ResourceExtension  One of the following: StaticResource Markup Extension, or DynamicResource Markup Extension. Unless the styles themselves contain references to potential run-time references such as system resources or user preferences, StaticResource Markup Extension reference to a style is usually recommended for performance.  TemplateResourceKey   x:Key Attribute string value referring to the template being requested as a resource. 

Property Value

A DataTemplate that specifies the visualization of the data objects. The default value is a null reference (Nothing in Visual Basic).

Identifier field

ItemTemplateProperty

Metadata properties set to true

None

You use the ItemTemplate to specify the visualization of the data objects. If your ItemsControl is bound to a collection object and you do not provide specific display instructions using a DataTemplate, the resulting UI of each item is a string representation of each object in the underlying collection.

When you set an ItemTemplate on an ItemsControl, this is how the UI gets generated (the following uses the ListBox as an example):

  1. During content generation, the ItemsPanel initiates a request for the ItemContainerGenerator to create a container for each data item. For ListBox, the container is a ListBoxItem. The generator calls back into the ItemsControl to prepare the container.

  2. Part of the preparation involves the copying of the ItemTemplate of the ListBox to be the ContentTemplate of the ListBoxItem.

  3. Similar to all ContentControl types, the ControlTemplate of a ListBoxItem contains a ContentPresenter. When the template is applied, it creates a ContentPresenter whose ControlTemplate is bound to the ControlTemplate of the ListBoxItem.

  4. Finally, the ContentPresenter applies that ControlTemplate to itself, and that creates the UI.

If you have more than one DataTemplate defined and you want to supply logic to programmatically choose and apply a DataTemplate, you use the ItemTemplateSelector property.

The ItemsControl provides great flexibility for visual customization, and therefore, provides many styling and templating properties. You use the ItemContainerStyle property or the ItemContainerStyleSelector property to set a style to affect the appearance of the elements that contain the data items. For example, for ListBox, the generated containers are ListBoxItem controls; for ComboBox, they are ComboBoxItem controls. To affect the layout of the items, you use the ItemsPanel property. If you are using grouping on your control, you can use GroupStyle or GroupStyleSelector.

For more information, see Data Templating Overview.

The following example shows how to create a DataTemplate inline. The DataTemplate specifies that each data item appears as three TextBlock elements within a StackPanel. In this example, the data object is a class called Task. Note that each TextBlock element in this template is bound to a property of the Task class.

<ListBox Width="400" Margin="10"
         ItemsSource="{Binding Source={StaticResource myTodoList}}">
   <ListBox.ItemTemplate>
     <DataTemplate>
       <StackPanel>
         <TextBlock Text="{Binding Path=TaskName}" />
         <TextBlock Text="{Binding Path=Description}"/>
         <TextBlock Text="{Binding Path=Priority}"/>
       </StackPanel>
     </DataTemplate>
   </ListBox.ItemTemplate>
 </ListBox>

It is more common to define a DataTemplate in the resources section so it can be a reusable object, as in the following example:

  <Window.Resources>

	...

    <DataTemplate x:Key="myTaskTemplate">
      <StackPanel>
        <TextBlock Text="{Binding Path=TaskName}" />
        <TextBlock Text="{Binding Path=Description}"/>
        <TextBlock Text="{Binding Path=Priority}"/>
      </StackPanel>
    </DataTemplate>

	...

</Window.Resources>

Now you can use myTaskTemplate as a resource, as in the following example:

<ListBox Width="400" Margin="10"
         ItemsSource="{Binding Source={StaticResource myTodoList}}"
         ItemTemplate="{StaticResource myTaskTemplate}"/>

For the complete sample, see Introduction to Data Templating Sample.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.