DataTemplateSelector Class


Enables custom template selection logic at the application level.

/// [Windows.Foundation.Metadata.Composable(Microsoft.UI.Xaml.Controls.IDataTemplateSelectorFactory, Windows.Foundation.Metadata.CompositionType.Public, 65536, "Microsoft.UI.Xaml.WinUIContract")]
/// [Windows.Foundation.Metadata.ContractVersion(Microsoft.UI.Xaml.WinUIContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
/// [Windows.Foundation.Metadata.WebHostHidden]
class DataTemplateSelector : IElementFactory
[Windows.Foundation.Metadata.Composable(typeof(Microsoft.UI.Xaml.Controls.IDataTemplateSelectorFactory), Windows.Foundation.Metadata.CompositionType.Public, 65536, "Microsoft.UI.Xaml.WinUIContract")]
[Windows.Foundation.Metadata.ContractVersion(typeof(Microsoft.UI.Xaml.WinUIContract), 65536)]
public class DataTemplateSelector : IElementFactory
Public Class DataTemplateSelector
Implements IElementFactory
See Remarks
Windows.Foundation.Metadata.ComposableAttribute Windows.Foundation.Metadata.ContractVersionAttribute Windows.Foundation.Metadata.MarshalingBehaviorAttribute Windows.Foundation.Metadata.ThreadingAttribute Windows.Foundation.Metadata.WebHostHiddenAttribute


For more info about data templates, see the DataTemplate class and Item containers and templates.

The base DataTemplateSelector class is not used as an object element in XAML. However, it is a common scenario to derive a custom DataTemplateSelector, map a xmlns prefix for the custom class and its namespace/assembly, and then refer to an instance of the custom class as defined in a Resources block in XAML. This makes it possible to refer to the custom template selector class by x:Key, and use that reference to set the value of properties such as ItemTemplateSelector in XAML templates and visual states.

The callable methods of DataTemplateSelector are the two SelectTemplate overloads. The overridable methods of DataTemplateSelector are the two SelectTemplateCore overloads. To define an effective DataTemplateSelector subclass, provide implementations for SelectTemplateCore(Object) and SelectTemplateCore(Object, DependencyObject). All these methods return a DataTemplate instance that's typically a specific choice that's appropriate for the inputs of the method that was called to obtain it.

App code typically doesn't call SelectTemplate methods; the methods exists so that the infrastructure can call it while choosing the correct templates based on using a DataTemplateSelector instance from a property value such as ItemsControl.ItemsTemplateSelector.

For example XAML that references a custom DataTemplateSelector as the HeaderTemplateSelector value, and example code that shows the overrides for HeaderTemplateSelector methods, see How to group items in a list or grid.



Initializes a new instance of the DataTemplateSelector class.



Creates or retrieves an existing instance of the UIElement object declared in the DataTemplate returned by SelectTemplate.


Recycles a UIElement object that was created using GetElement.


Returns a specific DataTemplate for a given item.

SelectTemplate(Object, DependencyObject)

Returns a specific DataTemplate for a given item and container.


When implemented by a derived class, returns a specific DataTemplate for a given item or container.

SelectTemplateCore(Object, DependencyObject)

When implemented by a derived class, returns a specific DataTemplate for a given item or container.

Applies to

See also