RadControls for Silverlight

What is a Template Selector?

The Template Selector provides a way to apply data templates based on custom logic.

CopyC#
public class DataTemplateSelector
CopyVB.NET (Declaration)
Public Class DataTemplateSelector
CopyVB.NET (Usage)
Dim instance As DataTemplateSelector
CopyXAML
<DataTemplateSelector .../>

Typically, you use a template selector when you have more than one data template defined for the same type of objects. For example, use it if your binding source is 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 by 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.

For more information read further in the topic.

How to Create a Template Selector?

To create a TemplateSelector that applies a data template based on custom logic, create a subclass of the DataTemplateSelector class and implement the SelectTemplate() method.

CopyC#
public virtual DataTemplate SelectTemplate(Object item, DependencyObject container)
CopyVB.NET
Public Overridable Function SelectTemplate ( _
    item As Object, _
    container As DependencyObject _
) As DataTemplate

Where the SelectTemplate() method accepts the following parameters:

  • item - the data object for which to select the data template.
  • container - the data-bound object.

The method returns a DataTemplate or a null reference (Nothing in Visual Basic). The default value is a null reference (Nothing in Visual Basic).

The following example shows how to define a TemplateSelector.

In this example, the binding source is a list of League objects. Each League object has a list of Division objects. Each Division object has a list of Team objects.

CopyC#
public class SampleTemplateSelector : DataTemplateSelector
{
    public override DataTemplate SelectTemplate( object item, DependencyObject container )
    {
        if ( item is League )
            return this.LeagueTemplate;
        else if ( item is Division )
            return this.DivisionTemplate;
        else if ( item is Team )
            return this.TeamTemplate;
        return null;
    }
    public DataTemplate LeagueTemplate
    {
        get;
        set;
    }
    public DataTemplate DivisionTemplate
    {
        get;
        set;
    }
    public DataTemplate TeamTemplate
    {
        get;
        set;
    }
}
CopyVB.NET
Public Class SampleTemplateSelector
    Inherits DataTemplateSelector
    Public Overloads Overrides Function SelectTemplate(ByVal item As Object, ByVal container As DependencyObject) As DataTemplate
        If TypeOf item Is League Then
            Return Me.LeagueTemplate
        ElseIf TypeOf item Is Division Then
            Return Me.DivisionTemplate
        ElseIf TypeOf item Is Team Then
            Return Me.TeamTemplate
        End If

        Return Nothing
    End Function
Private _LeagueTemplate As DataTemplate
    Public Property LeagueTemplate() As DataTemplate
        Get
            Return _LeagueTemplate
        End Get
        Set(ByVal value As DataTemplate)
            _LeagueTemplate = value
        End Set
    End Property
Private _DivisionTemplate As DataTemplate
    Public Property DivisionTemplate() As DataTemplate
        Get
            Return _DivisionTemplate
        End Get
        Set(ByVal value As DataTemplate)
            _DivisionTemplate = value
        End Set
    End Property
Private _TeamTemplate As DataTemplate
    Public Property TeamTemplate() As DataTemplate
        Get
            Return _TeamTemplate
        End Get
        Set(ByVal value As DataTemplate)
            _TeamTemplate = value
        End Set
    End Property
End Class

How to Apply a TemplateSelector

In order to be able to apply a template selector, you should first define a resource key for it.

CopyXAML
<example:SampleTemplateSelector x:Key="myTemplateSelector"
    LeagueTemplate="{StaticResource LeagueItemTemplate}"
    DivisionTemplate="{StaticResource DivisionItemTemplate}"
    TeamTemplate="{StaticResource TeamItemTemplate}"/>
Note

Note how the particular data templates (LeagueTemplate="{StaticResource LeagueItemTemplate}") are passed to the selector. If you want to use this approach you must declare the data template before the selector declaration.

Note

The example prefix maps to a CLR namespace and the corresponding assembly where the TemplateSelector is defined. For more information, see XAML Namespaces and Namespace Mapping.

The following example shows how to set the ItemTemplateSelector property of a RadTreeView to this TemplateSelector resource.

CopyXAML
<telerikNavigation:RadTreeView ItemTemplateSelector="{StaticResource myTemplateSelector}"/>