Progress® Telerik® UI for Windows Phone

This topic explains how to use the ItemTemplateSelector property to enable dynamic templating of slide view items.

Creating a template selector

We will create our template selector by inheriting the Telerik.Windows.Controls.DataTemplateSelector class, residing in the Telerik.Windows.Core assembly.

CopyC#
public class TemplateSelector : DataTemplateSelector
{
    public DataTemplate DefaultTemplate
    {
        get;
        set;
    }

    public DataTemplate SelectedTemplate
    {
        get;
        set;
    }

    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        RadSlideView view = ElementTreeHelper.FindVisualAncestor<RadSlideView>(container);
        if (view == null)
        {
            return null;
        }

        if (object.Equals(item, view.SelectedItem))
        {
            return this.SelectedTemplate;
        }

        return this.DefaultTemplate;
    }
}

Setting-up the XAML

Now let's instantiate our new selector in XAML and specify its SelectedTemplate and DefaultTemplate properties:

CopyXAML
<UserControl.Resources>
    <DataTemplate x:Key="selectedTemplate">
        <Border Background="{StaticResource PhoneSubtleBrush}">
            <TextBlock Text="{Binding}" FontSize="{StaticResource PhoneFontSizeLarge}"
                       HorizontalAlignment="Center" VerticalAlignment="Center"/>
        </Border>
    </DataTemplate>
    <DataTemplate x:Key="defaultTemplate">
        <Border>
            <TextBlock Text="{Binding}" FontSize="{StaticResource PhoneFontSizeNormal}"
                       HorizontalAlignment="Center" VerticalAlignment="Center"/>
        </Border>
    </DataTemplate>
    <local:TemplateSelector x:Key="templateSelector" 
                            DefaultTemplate="{StaticResource defaultTemplate}" 
                            SelectedTemplate="{StaticResource selectedTemplate}"/>
</UserControl.Resources>

Applying the selector

Finally we need to apply the selector to a RadSlideView instance:

CopyXAML
<telerikPrimitives:RadSlideView x:Name="radSlideView" 
                                ItemsSource="{Binding}" 
                                ItemTemplateSelector="{StaticResource templateSelector}">
</telerikPrimitives:RadSlideView>