ItemsControl.ItemTemplateSelector 属性


获取或设置用于选择用来显示每个项的模板的自定义逻辑。Gets or sets the custom logic for choosing a template used to display each item.

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



自定义 DataTemplateSelector 对象,它提供逻辑并返回 DataTemplateA custom DataTemplateSelector object that provides logic and returns a DataTemplate. 默认为 nullThe default is null.



在下面的示例中, auctionItemDataTemplateSelector 资源名称 (对应于 AuctionItemDataTemplateSelector 类) 分配给的 ItemTemplateSelector 属性 ItemsControlIn the following example, the auctionItemDataTemplateSelector resource name (corresponding to an AuctionItemDataTemplateSelector class) is assigned to the ItemTemplateSelector property of the ItemsControl.

   Template="{StaticResource ScrollTemplate}" 
   ItemsSource="{Binding Source={StaticResource items_list}}"
   ItemTemplateSelector="{StaticResource auctionItemDataTemplateSelector}" >
         <StackPanel />

下面的示例演示如何 AuctionItemDataTemplateSelector 通过重写方法来实现类 SelectTemplateThe following example shows the implementation of the AuctionItemDataTemplateSelector class with an override of the SelectTemplate method:

using System.Windows;
using System.Windows.Controls;

namespace SDKSample
    public class AuctionItemDataTemplateSelector : DataTemplateSelector
        public override DataTemplate 
            SelectTemplate(object item, DependencyObject container)
            FrameworkElement element = container as FrameworkElement;

            if (element != null && item != null && item is AuctionItem)
                AuctionItem auctionItem = item as AuctionItem;
                Window window = Application.Current.MainWindow;

                switch (auctionItem.SpecialFeatures)
                    case SpecialFeatures.None:
                            as DataTemplate;
                    case SpecialFeatures.Color:
                            as DataTemplate;

            return null;

Namespace SDKSample
    Public Class AuctionItemDataTemplateSelector
        Inherits DataTemplateSelector
        Public Overrides Function SelectTemplate(ByVal item As Object, ByVal container As DependencyObject) As DataTemplate

            Dim element As FrameworkElement = TryCast(container, FrameworkElement)

            If element isnot Nothing andalso item IsNot Nothing AndAlso TypeOf item Is AuctionItem Then

                Dim auctionItem As AuctionItem = TryCast(item, AuctionItem)

                Select Case auctionItem.SpecialFeatures
                    Case SpecialFeatures.None
                        Return TryCast(element.FindResource("AuctionItem_None"), DataTemplate)
                    Case SpecialFeatures.Color
                        Return TryCast(element.FindResource("AuctionItem_Color"), DataTemplate)
                End Select
            End If

            Return Nothing
        End Function
    End Class
End Namespace

在这种情况下,在 SelectTemplate 类的方法中,有一个逻辑,根据 SpecialFeatures 传递的对象的属性值返回相应的模板 itemIn this case, within the SelectTemplate method of the class, there is logic to return the appropriate template based on the value of the SpecialFeatures property of the item object passed. 在封装元素的资源中找到要返回的模板 WindowThe template to return is found in the resources of the enveloping Window element.

设置 ItemTemplateSelector 属性时,将定向到,以便 ItemsControl SelectTemplate AuctionItemDataTemplateSelector 为绑定到的集合中的每个项自动调用的方法 ItemsControlWhen you set the ItemTemplateSelector property, the ItemsControl is directed to automatically call the SelectTemplate method of AuctionItemDataTemplateSelector for each of the items in the collection to which the ItemsControl is bound. 调用以对象的形式传递数据项。The call passes the data item as an object. 然后,将 DataTemplate 使用方法返回的来显示该数据项。The DataTemplate that is returned by the method is then used to display that data item.

有关其他示例,请参阅 数据模板化概述For another example, see Data Templating Overview.


您可以使用 ItemTemplate 指定数据对象的可视化。You use the ItemTemplate to specify the visualization of the data objects. 如果定义了多个模板,并且想要提供逻辑来返回要使用的模板,则使用此属性。If you have more than one template defined and want to supply logic to return a template to use, then you use this property. 请注意,如果设置了,则忽略此属性 ItemTemplateNote that this property is ignored if ItemTemplate is set.

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. 如果对控件使用分组,则可以使用 GroupStyleGroupStyleSelector 属性。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 ItemTemplateSelector="ResourceExtension SelectorResourceKey"/>  


以下项之一: StaticResource 、或 DynamicResourceOne of the following: StaticResource, or DynamicResource. 除非样式本身包含对潜在运行时引用(如系统资源或用户首选项)的引用,否则 StaticResource 通常建议对样式的引用以提高性能。Unless the styles themselves contain references to potential run-time references such as system resources or user preferences, StaticResource reference to a style is usually recommended for performance.

x:Key 引用作为资源请求的选择器的字符串值。x:Key string value referring to the selector being requested as a resource.

依赖项属性信息Dependency Property Information

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