Click here to Skip to main content
Click here to Skip to main content

Tagged as

Silverlight DataTemplateSelector

, 7 Jul 2010
Rate this:
Please Sign up or sign in to vote.
Implementing DataTemplateSelector on Silverlight

Introduction

One of the cool features of WPF is the ability of apply a Template to a chunk of data according to your own criteria. This is achieved using an implementation of the DataTemplateSelector class. In Silverlight, we are missing this ability of using a DataTemplateSelector and because of this, we have to use more code to fill this hole.

Background

The central idea of this article is to implement a way to use this missing feature by creating a DataTemplateSelector class.

Using the Code

The concept is fairly simple, you have an abstract class, extending from ContentControl that will invoke the virtual method SelectTemplate. The idea is to mimic as much as possible the DataTemplateSelector so that we will have no problem implementing it and reusing it.

The abstract class is simple:

public abstract class DataTemplateSelector : ContentControl
{
    public virtual DataTemplate SelectTemplate(
        object item, DependencyObject container)
    {
        return null;
    }

    protected override void OnContentChanged(
        object oldContent, object newContent)
    {
        base.OnContentChanged(oldContent, newContent);

        ContentTemplate = SelectTemplate(newContent, this);
    }
}

Then for our use, as it is in WPF we have to create a class that will have the specific SelectTemplate override that will apply the Template to the specified data chunk. Here is an example:

public class CountryTemplateSelector : DataTemplateSelector
{
    public DataTemplate BrazilTemplate
    {
        get;
        set;
    }

    public DataTemplate UsaTemplate
    {
        get;
        set;
    }

    public DataTemplate EnglandTemplate
    {
        get;
        set;
    }

    public override DataTemplate SelectTemplate(
        object item, DependencyObject container)
    {
        City itemAux = item as City;
        if (itemAux != null)
        {
            if (itemAux.Country == "Brazil")
                return BrazilTemplate;
            if (itemAux.Country == "USA")
                return UsaTemplate;
            if (itemAux.Country == "England")
                return EnglandTemplate;
        }

        return base.SelectTemplate(item, container);
    }
}

So if the content of the CountryTemplateSelector is a City, it will return our template based on the country verification, returning the needed template for the class, like in WPF. Of course, since the article only shows the concept I have made a very simple verification, but you can add any logic to your SelectTemplate method.

As I said, fairly simple, like in WPF. But in Silverlight, there will be differences, of course, because the missing ItemTemplateSelector, ContentTemplateSelector,... properties in the controls.

The differences are also minor, instead of using a property that will hold the reference to the StaticResource that represents the DataTemplateSelector in our ResourceDictionary, we will have to add the class as the content of a DataTemplate like this:

<ListBox ItemsSource="{Binding Cities}" HorizontalContentAlignment="Stretch">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <local:CountryTemplateSelector Content="{Binding}">
                <local:CountryTemplateSelector.BrazilTemplate>
                    <DataTemplate>
                        <Grid Background="Green">
                            <TextBlock Text="{Binding Name}"/>
                        </Grid>
                    </DataTemplate>
                </local:CountryTemplateSelector.BrazilTemplate>
                <local:CountryTemplateSelector.UsaTemplate>
                    <DataTemplate>
                        <Grid Background="Blue">
                            <TextBlock Text="{Binding Name}"/>
                        </Grid>
                    </DataTemplate>
                </local:CountryTemplateSelector.UsaTemplate>
                <local:CountryTemplateSelector.EnglandTemplate>
                    <DataTemplate>
                        <Grid Background="Red">
                            <TextBlock Text="{Binding Name}"/>
                        </Grid>
                    </DataTemplate>
                </local:CountryTemplateSelector.EnglandTemplate>
            </local:CountryTemplateSelector>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

And there you go. Your control rendering the template needed for the needed scenario.

Thanks

This code is a simple way to apply a very flexible functionality. Hope this aids your development and makes your life a little bit easy. Don't forget to drop a message having your say, really willing to hear more opinions.

Best regards.

History

  • 7th July, 2010: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Raul Mainardi Neto
Architect
Brazil Brazil
Senior .NET Architect from Brazil.
Follow on   Twitter

Comments and Discussions

 
GeneralSimple solution for static templates only PinmemberTodd Beaulieu19-Mar-14 1:46 
QuestionMy Vote PinprofessionalMycroft Holmes22-Jan-14 22:18 
QuestionGood! Thanks! Pinmembersangolee19-Jan-14 15:09 
BugDefault style inheritance Pinmemberkataik6-Mar-13 10:11 
Question5 Starts post PinmemberMember 85990391-Feb-12 7:00 
GeneralMy vote of 5 PinmemberMarkus-81119-Jan-12 18:51 
GeneralMy vote of 5 Pinmemberfabiowitt3-Jun-11 4:28 
GeneralVery Good Raul Pinmemberfabiowitt3-Jun-11 4:27 
GeneralUpdating templates PinmemberMisterTown5-May-11 23:26 
GeneralRe: Updating templates PinmemberMichael.Sh879-Jan-12 23:17 
GeneralHierarchialDataTemplate PinmemberJurie Smit30-Nov-10 11:17 
GeneralRe: HierarchialDataTemplate Pinmemberda_ponc1-Jan-11 21:06 
Hi Jurie,
 
I've just encountered the same problem (ie the ItemsSource property of HierarchicalDataTemplate not getting called). Did you manage to find a solution?
 
Cheers.
GeneralRe: HierarchialDataTemplate PinmemberJurie Smit2-Jan-11 20:58 
QuestionError in design view Pinmember5774736-Sep-10 7:12 
GeneralA reusable datatemplate PinmemberEric Pizon28-Jul-10 9:14 
GeneralRe: A reusable datatemplate [modified] PinmemberRaul Mainardi Neto28-Jul-10 9:25 
GeneralRe: A reusable datatemplate PinmemberMember 408773628-Dec-10 14:25 
GeneralRe: A reusable datatemplate Pinmembergoliknes22-Jan-11 5:26 
GeneralRe: A reusable datatemplate PinmemberRaul Mainardi Neto24-Jan-11 15:43 
GeneralMy vote of 5 PinmemberMarcelo Ricardo de Oliveira12-Jul-10 6:52 
GeneralRe: My vote of 5 PinmemberRaul Mainardi Neto12-Jul-10 6:53 
GeneralMy vote of 5 PinmentorKunalChowdhury9-Jul-10 22:43 
GeneralRe: My vote of 5 PinmemberRaul Mainardi Neto12-Jul-10 1:55 
GeneralMy vote of 5 Pinmembervikas amin9-Jul-10 10:19 
GeneralRe: My vote of 5 PinmemberRaul Mainardi Neto9-Jul-10 15:12 
GeneralNice one Raul PinmvpSacha Barber8-Jul-10 2:55 
GeneralRe: Nice one Raul PinmemberRaul Mainardi Neto8-Jul-10 3:13 
GeneralRe: Nice one Raul PinmvpSacha Barber8-Jul-10 3:33 
GeneralRe: Nice one Raul PinmemberRaul Mainardi Neto8-Jul-10 4:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 7 Jul 2010
Article Copyright 2010 by Raul Mainardi Neto
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid