ItemsControl.ItemTemplate 属性


获取或设置用来显示每个项的 DataTemplateGets or sets the DataTemplate used to display each item.

 property System::Windows::DataTemplate ^ ItemTemplate { System::Windows::DataTemplate ^ get(); void set(System::Windows::DataTemplate ^ value); };
public System.Windows.DataTemplate ItemTemplate { get; set; }
member this.ItemTemplate : System.Windows.DataTemplate with get, set
Public Property ItemTemplate As DataTemplate


指定数据对象的可视化的 DataTemplateA DataTemplate that specifies the visualization of the data objects. 默认值为 nullThe default is null.



下面的示例演示如何创建 DataTemplate 内联。The following example shows how to create a DataTemplate inline. DataTemplate 指定每个数据项在 StackPanel内显示为三个 TextBlock 元素。The DataTemplate specifies that each data item appears as three TextBlock elements within a StackPanel. 在此示例中,数据对象是一个名为 Task的类。In this example, the data object is a class called Task. 请注意,此模板中的每个 TextBlock 元素都绑定到 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}}">
         <TextBlock Text="{Binding Path=TaskName}" />
         <TextBlock Text="{Binding Path=Description}"/>
         <TextBlock Text="{Binding Path=Priority}"/>

在 resources 节中定义一个 DataTemplate 更常见,因此它可以是一个可重用对象,如下例所示:It is more common to define a DataTemplate in the resources section so it can be a reusable object, as in the following example:

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

现在可以将 myTaskTemplate 用作资源,如以下示例所示: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.


使用 ItemTemplate 指定数据对象的可视化。You use the ItemTemplate to specify the visualization of the data objects. 如果 ItemsControl 绑定到集合对象,但未使用 DataTemplate提供特定显示说明,则每个项的生成 UIUI 都是基础集合中每个对象的字符串表示形式。If your ItemsControl is bound to a collection object and you do not provide specific display instructions using a DataTemplate, the resulting UIUI of each item is a string representation of each object in the underlying collection.

ItemsControl上设置 ItemTemplate 时,UIUI 按如下方式生成(使用 ListBox 作为示例):When you set an ItemTemplate on an ItemsControl, the UIUI is generated as follows (using the ListBox as an example):

  1. 在内容生成过程中,ItemsPanel 启动 ItemContainerGenerator 的请求,以便为每个数据项创建容器。During content generation, the ItemsPanel initiates a request for the ItemContainerGenerator to create a container for each data item. 对于 ListBox,容器为 ListBoxItemFor ListBox, the container is a ListBoxItem. 生成器将回调到 ItemsControl 来准备容器。The generator calls back into the ItemsControl to prepare the container.

  2. 部分准备工作涉及将 ListBox ItemTemplate 复制到 ListBoxItemContentTemplatePart of the preparation involves the copying of the ItemTemplate of the ListBox to be the ContentTemplate of the ListBoxItem.

  3. 与所有 ContentControl 类型类似,ListBoxItemControlTemplate 包含 ContentPresenterSimilar to all ContentControl types, the ControlTemplate of a ListBoxItem contains a ContentPresenter. 应用模板时,它将创建一个 ContentPresenter,其 ContentTemplate 绑定到 ListBoxItemContentTemplateWhen the template is applied, it creates a ContentPresenter whose ContentTemplate is bound to the ContentTemplate of the ListBoxItem.

  4. 最后,ContentPresenter 将该 ContentTemplate 应用于自身,这将创建 UIUIFinally, the ContentPresenter applies that ContentTemplate to itself, and that creates the UIUI.

如果定义了多个 DataTemplate 并且想要提供逻辑来以编程方式选择和应用 DataTemplate,请使用 ItemTemplateSelector 属性。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.

ItemsControl 为视觉对象自定义提供了极大的灵活性,并提供了许多样式设置和模板化属性。The ItemsControl provides great flexibility for visual customization and provides many styling and templating properties. 使用 ItemContainerStyle 属性或 ItemContainerStyleSelector 属性设置样式,以影响包含数据项的元素的外观。Use the ItemContainerStyle property or the ItemContainerStyleSelector property to set a style to affect the appearance of the elements that contain the data items. 例如,对于 ListBox,生成的容器是 ListBoxItem 控件;对于 ComboBox,它们是 ComboBoxItem 控件。For example, for ListBox, the generated containers are ListBoxItem controls; for ComboBox, they are ComboBoxItem controls. 若要影响项的布局,请使用 ItemsPanel 属性。To affect the layout of the items, use the ItemsPanel property. 如果对控件使用分组,则可以使用 "GroupStyle" 或 "GroupStyleSelector" 属性。If you are using grouping on your control, you can use the GroupStyle or GroupStyleSelector property.

有关详细信息,请参阅 数据模板化概述For more information, see Data Templating Overview.

XAML 特性用法XAML Attribute Usage

<object ItemTemplate=" ResourceExtension TemplateResourceKey"/>  


以下项之一: StaticResource 标记扩展DynamicResource 标记扩展One of the following: StaticResource Markup Extension, or DynamicResource Markup Extension. 除非样式本身包含对潜在运行时引用(如系统资源或用户首选项)的引用,否则,通常建议使用样式的StaticResource 标记扩展引用来提高性能。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.

用于引用作为资源请求的模板的X:Key 指令字符串值。x:Key Directive string value referring to the template being requested as a resource.

依赖项属性信息Dependency Property Information

标识符字段Identifier field ItemTemplateProperty
元数据属性设置为 trueMetadata properties set to true None