DataTemplateSelector.SelectTemplate Method (System.Windows.Controls)

Switch View :
ScriptFree
.NET Framework Class Library
DataTemplateSelector.SelectTemplate Method

When overridden in a derived class, returns a DataTemplate based on custom logic.

Namespace:  System.Windows.Controls
Assembly:  PresentationFramework (in PresentationFramework.dll)
Syntax

Visual Basic
Public Overridable Function SelectTemplate ( _
	item As Object, _
	container As DependencyObject _
) As DataTemplate
C#
public virtual DataTemplate SelectTemplate(
	Object item,
	DependencyObject container
)
Visual C++
public:
virtual DataTemplate^ SelectTemplate(
	Object^ item, 
	DependencyObject^ container
)
F#
abstract SelectTemplate : 
        item:Object * 
        container:DependencyObject -> DataTemplate 
override SelectTemplate : 
        item:Object * 
        container:DependencyObject -> DataTemplate 

Parameters

item
Type: System.Object
The data object for which to select the template.
container
Type: System.Windows.DependencyObject
The data-bound object.

Return Value

Type: System.Windows.DataTemplate
Returns a DataTemplate or Nothing. The default value is Nothing.
Remarks

Typically, you use a template selector when you have more than one data template defined for the same type of objects. For example, if your binding source is list a list of student objects and you want to apply a particular template to the part-time students. You can do this by creating a class that inherits from DataTemplateSelector and overriding the SelectTemplate method. Once your class is defined you can assign an instance of the class to the template selector property of your element.

Examples

In this example, the binding source is a list of Task objects. One of the properties of a Task object is Priority. There are two data templates defined, myTaskTemplate and importantTaskTemplate.

To supply logic to choose which DataTemplate to use based on the Priority value of the data object, create a subclass of DataTemplateSelector and override the SelectTemplate method. In the following example, the SelectTemplate method provides logic to return the appropriate template based on the value of the Priority property. The template to return is found in the resources of the enveloping Window element.

Visual Basic


Namespace SDKSample
	Public Class TaskListDataTemplateSelector
		Inherits DataTemplateSelector
		Public Overrides Function SelectTemplate(ByVal item As Object, ByVal container As DependencyObject) As DataTemplate

            Dim element As FrameworkElement
            element = TryCast(container, FrameworkElement)

            If element IsNot Nothing AndAlso item IsNot Nothing AndAlso TypeOf item Is Task Then

                Dim taskitem As Task = TryCast(item, Task)

                If taskitem.Priority = 1 Then
                    Return TryCast(element.FindResource("importantTaskTemplate"), DataTemplate)
                Else
                    Return TryCast(element.FindResource("myTaskTemplate"), DataTemplate)
                End If
            End If

			Return Nothing
		End Function
	End Class
End Namespace


C#

using System.Windows;
using System.Windows.Controls;

namespace SDKSample
{
    public class TaskListDataTemplateSelector : DataTemplateSelector
    {
        public override DataTemplate
            SelectTemplate(object item, DependencyObject container)
        {
            FrameworkElement element = container as FrameworkElement;

            if (element != null && item != null && item is Task)
            {
                Task taskitem = item as Task;

                if (taskitem.Priority == 1)
                    return
                        element.FindResource("importantTaskTemplate") as DataTemplate;
                else
                    return
                        element.FindResource("myTaskTemplate") as DataTemplate;
            }

            return null;
        }
    }
}


We can then declare the TaskListDataTemplateSelector as a resource:

XAML

<Window.Resources>


...


<local:TaskListDataTemplateSelector x:Key="myDataTemplateSelector"/>


...


</Window.Resources>


To use the template selector resource, assign it to the ItemTemplateSelector property of the ListBox. The ListBox calls the SelectTemplate method of the TaskListDataTemplateSelector for each of the items in the underlying collection. The call passes the data object as the item parameter. The DataTemplate that is returned by the method is then applied to that data object.

XAML

<ListBox Width="400" Margin="10"
         ItemsSource="{Binding Source={StaticResource myTodoList}}"
         ItemTemplateSelector="{StaticResource myDataTemplateSelector}"
         HorizontalContentAlignment="Stretch"/>


For the complete sample, see Introduction to Data Templating Sample.

Version Information

.NET Framework

Supported in: 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Platforms

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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

Reference

Other Resources