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; }
[<System.ComponentModel.Bindable(true)>]
member this.ItemTemplateSelector : System.Windows.Controls.DataTemplateSelector with get, set
Public Property ItemTemplateSelector As DataTemplateSelector

屬性值

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.

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

下列範例示範如何使用方法的覆 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:
                        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 類別的方法中,有邏輯可以根據 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 Attribute UsageXAML Attribute Usage

<object ItemTemplateSelector="ResourceExtension SelectorResourceKey"/>  

XAML 值XAML Values

ResourceExtensionResourceExtension
下列其中一項: 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.

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

適用於

另請參閱