ItemsControl.ItemTemplateSelector 属性

定义

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

public:
 property System::Windows::Controls::DataTemplateSelector ^ ItemTemplateSelector { System::Windows::Controls::DataTemplateSelector ^ get(); void set(System::Windows::Controls::DataTemplateSelector ^ value); };
[System.ComponentModel.Bindable(true)]
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 类)分配给 ItemsControlItemTemplateSelector 属性。In the following example, the auctionItemDataTemplateSelector resource name (corresponding to an AuctionItemDataTemplateSelector class) is assigned to the ItemTemplateSelector property of the ItemsControl.

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

下面的示例演示如何使用 SelectTemplate 方法的重写来实现 AuctionItemDataTemplateSelector 类:The 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:
                        return 
                            element.FindResource("AuctionItem_None") 
                            as DataTemplate;
                    case SpecialFeatures.Color:
                        return 
                            element.FindResource("AuctionItem_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 方法中,有一个逻辑,根据传递的 item 对象的 SpecialFeatures 属性的值返回相应的模板。In 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. 在封装 Window 元素的资源中找到要返回的模板。The template to return is found in the resources of the enveloping Window element.

设置 ItemTemplateSelector 属性时,ItemsControl 将被定向到为该 ItemsControl 绑定到的集合中的每个项自动调用 AuctionItemDataTemplateSelectorSelectTemplate 方法。When 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. 请注意,如果设置 ItemTemplate,则忽略此属性。Note 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. 如果对控件使用分组,则可以使用 "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 ItemTemplateSelector="ResourceExtension SelectorResourceKey"/>  

XAML 值XAML Values

ResourceExtensionResourceExtension
以下项之一: StaticResourceDynamicResourceOne 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.

SelectorResourceKeySelectorResourceKey
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

适用于

另请参阅