Data​Template​Selector Data​Template​Selector Data​Template​Selector Class

Definition

Enables custom template selection logic at the application level.

public class DataTemplateSelector : IDataTemplateSelector, IDataTemplateSelector2, IDataTemplateSelectorOverrides, IDataTemplateSelectorOverrides2public class DataTemplateSelector : IDataTemplateSelector, IDataTemplateSelector2, IDataTemplateSelectorOverrides, IDataTemplateSelectorOverrides2Public Class DataTemplateSelector Implements IDataTemplateSelector, IDataTemplateSelector2, IDataTemplateSelectorOverrides, IDataTemplateSelectorOverrides2
See Remarks
Attributes
Windows 10 requirements
Device family
Windows 10 (introduced v10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced v1)

Remarks

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.

Constructors

DataTemplateSelector() DataTemplateSelector() DataTemplateSelector()

Initializes a new instance of the DataTemplateSelector class.

public DataTemplateSelector()public DataTemplateSelector()Public Sub New()
Attributes

Methods

SelectTemplate(Object) SelectTemplate(Object) SelectTemplate(Object)

Returns a specific DataTemplate for a given item.

public DataTemplate SelectTemplate(Object item)public DataTemplate SelectTemplate(Object item)Public Function SelectTemplate(item As Object) As DataTemplate
Parameters
item
System.Object System.Object System.Object

The item to return a template for.

Returns

The template to use for the given item and/or container.

Attributes

SelectTemplate(Object, DependencyObject) SelectTemplate(Object, DependencyObject) SelectTemplate(Object, DependencyObject)

Returns a specific DataTemplate for a given item and container.

public DataTemplate SelectTemplate(Object item, DependencyObject container)public DataTemplate SelectTemplate(Object item, DependencyObject container)Public Function SelectTemplate(item As Object, container As DependencyObject) As DataTemplate
Parameters
item
System.Object System.Object System.Object

The item to return a template for.

container
DependencyObject DependencyObject DependencyObject

The parent container for the templated item.

Returns

The template to use for the given item and/or container.

Attributes
See Also

SelectTemplateCore(Object) SelectTemplateCore(Object) SelectTemplateCore(Object)

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

protected virtual DataTemplate SelectTemplateCore(Object item)protected virtual DataTemplate SelectTemplateCore(Object item)Protected Overridable Function SelectTemplateCore(item As Object) As DataTemplate
Parameters
item
System.Object System.Object System.Object

The item to return a template for.

Returns

The template to use for the given item and/or container.

Attributes

Remarks

If your ItemsControl.ItemsPanel is an ItemsStackPanel or ItemsWrapGrid, provide an override for the SelectTemplateCore(Object) method. If the ItemsPanel is a different panel, such as VirtualizingStackPanel or WrapGrid, provide an override for the SelectTemplateCore(Object, DependencyObject) method.

Note

In Windows 8.1, ItemsStackPanel is the default ItemsPanel for ListView, and ItemsWrapGrid is the default ItemsPanel for GridView.

SelectTemplateCore(Object, DependencyObject) SelectTemplateCore(Object, DependencyObject) SelectTemplateCore(Object, DependencyObject)

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

protected virtual DataTemplate SelectTemplateCore(Object item, DependencyObject container)protected virtual DataTemplate SelectTemplateCore(Object item, DependencyObject container)Protected Overridable Function SelectTemplateCore(item As Object, container As DependencyObject) As DataTemplate
Parameters
item
System.Object System.Object System.Object

The item to return a template for.

container
DependencyObject DependencyObject DependencyObject

The parent container for the templated item.

Returns

The template to use for the given item and/or container.

Attributes

Remarks

Most implementations will choose to implement the selection logic based on the value of either item or container, not both. Implementations should still pass the unused parameter as-is to base.

See Also

See Also