ItemsControl.ItemTemplateSelector Property


取得或設定選擇用來顯示每個項目範本的自訂邏輯。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

Property Value


自訂 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.

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

下列範例示範如何使用 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:
                            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 方法中,有邏輯可以根據所傳遞 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. 如果您在控制項上使用群組,您可以使用 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 Attribute UsageXAML 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
設定為 true 的中繼資料屬性Metadata properties set to true NoneNone

Applies to

See also