Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

ItemsControl.ItemTemplateSelector Property

Allows the application writer to provide custom logic for choosing a template used to display each item. This is a dependency property.

Namespace: System.Windows.Controls
Assembly: PresentationFramework (in presentationframework.dll)
XML Namespace:  http://schemas.microsoft.com/winfx/2006/xaml/presentation

[BindableAttribute(true)] 
public DataTemplateSelector ItemTemplateSelector { get; set; }
/** @property */
public DataTemplateSelector get_ItemTemplateSelector ()

/** @property */
public void set_ItemTemplateSelector (DataTemplateSelector value)

public function get ItemTemplateSelector () : DataTemplateSelector

public function set ItemTemplateSelector (value : DataTemplateSelector)

<object>
  <object.ItemTemplateSelector>
    <DataTemplateSelector .../>
  </object.ItemTemplateSelector>
</object>
<object ItemTemplateSelector="ResourceExtensionSelectorResourceKey"/>
 ResourceExtension  One of the following: StaticResource, or DynamicResource. Unless the styles themselves contain references to potential run-time refererences 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

A custom DataTemplateSelector object that provides logic and returns a DataTemplate. The default value is a null reference (Nothing in Visual Basic).

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 therefore, provides many styling and templating properties. You 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, you use the ItemsPanel property. If you are using grouping on your control, you can use GroupStyle or GroupStyleSelector.

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)
        {
            if (item != null && item is AuctionItem)
            {
                AuctionItem auctionItem = item as AuctionItem;
                Window window = Application.Current.MainWindow;

                switch (auctionItem.SpecialFeatures)
                {
                    case SpecialFeatures.None:
                        return 
                            window.FindResource("AuctionItem_None") 
                            as DataTemplate;
                    case SpecialFeatures.Color:
                        return 
                            window.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.

By assigning 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.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0

Community Additions

Show:
© 2015 Microsoft