ItemsControl.ItemTemplateSelector Property

Gets or sets the custom logic for choosing a template used to display each item.

Namespace:  System.Windows.Controls
Assembly:  PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation

[BindableAttribute(true)]
public DataTemplateSelector ItemTemplateSelector { get; set; }
<object>
  <object.ItemTemplateSelector>
    <DataTemplateSelector .../>
  </object.ItemTemplateSelector>
</object>
<object ItemTemplateSelector="ResourceExtension SelectorResourceKey"/>

XAML Values

ResourceExtension

One of the following: StaticResource, or DynamicResource. 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.

SelectorResourceKey

x:Key string value referring to the selector being requested as a resource.

Property Value

Type: System.Windows.Controls.DataTemplateSelector
A custom DataTemplateSelector object that provides logic and returns a DataTemplate. The default is null.

Identifier field

ItemTemplateSelectorProperty

Metadata properties set to true

None

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. Note that this property is ignored if ItemTemplate is set.

The ItemsControl provides great flexibility for visual customization and provides many styling and templating properties. Use the ItemContainerStyle property or the ItemContainerStyleSelector property to set a style to affect the appearance of the elements that contain the data items. For example, for ListBox, the generated containers are ListBoxItem controls; for ComboBox, they are ComboBoxItem controls. To affect the layout of the items, use the ItemsPanel property. If you are using grouping on your control, you can use the GroupStyle or GroupStyleSelector property.

For more information, see Data Templating Overview.

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>

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;
        }
    }


}

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. The template to return is found in the resources of the enveloping Window element.

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. The DataTemplate that is returned by the method is then used to display that data item.

For another example, see Data Templating Overview.

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft