แก้ไข

แชร์ผ่าน


ItemsControl.ItemTemplate Property

Definition

Gets or sets the DataTemplate used to display each item.

public:
 property System::Windows::DataTemplate ^ ItemTemplate { System::Windows::DataTemplate ^ get(); void set(System::Windows::DataTemplate ^ value); };
[System.ComponentModel.Bindable(true)]
public System.Windows.DataTemplate ItemTemplate { get; set; }
[<System.ComponentModel.Bindable(true)>]
member this.ItemTemplate : System.Windows.DataTemplate with get, set
Public Property ItemTemplate As DataTemplate

Property Value

A DataTemplate that specifies the visualization of the data objects. The default is null.

Attributes

Examples

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.

Remarks

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, the UI is generated as follows (using 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 ContentTemplate is bound to the ContentTemplate of the ListBoxItem.

  4. Finally, the ContentPresenter applies that ContentTemplate 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, use the ItemTemplateSelector property.

The ItemsControl provides great flexibility for visual customization and provides many styling and templating properties. 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, use the ItemsPanel property. If you are using grouping on your control, you can use the GroupStyle or GroupStyleSelector property.

For more information, see Data Templating Overview.

XAML Attribute Usage

<object ItemTemplate=" ResourceExtension TemplateResourceKey"/>

XAML Values

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 Directive string value referring to the template being requested as a resource.

Dependency Property Information

Identifier field ItemTemplateProperty
Metadata properties set to true None

Applies to

See also