Click here to Skip to main content
14,977,701 members
Articles / Desktop Programming / WPF
Article
Posted 9 Jul 2012

Tagged as

Stats

107.9K views
2.7K downloads
30 bookmarked

WPF Based Dynamic DataTemplateSelector

Rate me:
Please Sign up or sign in to vote.
4.94/5 (7 votes)
9 Jul 2012CPOL4 min read
Demonstrates how to make use of a completely WPF based DataTemplateSelector relying on no code-behind.

Introduction

This article will cover a method for defining different DataTemplateSelector options within your WPF code and allowing a very small amount of backend code to take care of all the selections for you. This allows you to make use of this code from within an MVVM structure without any issues, and while making the front end extremely easy to manage without complicating the back end code.

Background

Recently I was working on a project in which I wanted to implement a list where the contents could be one of a number of different types. Obviously, I wouldn't know what the contents of this list were beforehand (and as such I couldn't just statically create the content) so I started looking at ways to bend the ItemsTemplate property to do my nefarious bidding. One of the first things I noticed was ItemsTemplateSelector which sounded promising, but upon reading up a bit on it I found that it encouraged the use of code behind to select the data templates.

The way you are expected to do this, for those of you who are curious and a bit too lazy to go looking elsewhere for an example, is documented below.

DataTemplate Usage

WPF Code

XML
<local:TemplateSelector x:Key="MyTemplateSelector" />

<Style x:Key="MyListStyle" TargetType="ListView">
    <Setter Property="ItemTemplateSelector" Value="{StaticResource MyTemplateSelector}"/>
</Style>

Code Behind

C#
public class TemplateSelector : DataTemplateSelector
{
    DataTemplate stringTemplate; //This would need to be initialized

    //You override this function to select your data template based in the given item
    public override System.Windows.DataTemplate SelectTemplate(object item, 
                    System.Windows.DependencyObject container)
    {
        if(item is string)
            return stringTemplate;
        else
            return base.SelectTemplate(item, container);
    }
}

Obviously, this is an extremely simplified version of what you would end up doing, but it should give you an idea of what is going on.

Since I was using Caliburn.Micro (something I would highly recommend you start using if you aren't already) I was a bit apprehensive of using anything where my back end code would need to grab data templates from my front end (UI). That being said, I wasn't going to be too stubborn, but I am also an inherently lazy person, and I don't like having to do extra work to get things working, something it appeared I would need to do to get a hold of the templates I was trying to supply. The issue is that the DataTemplateSelector doesn't actually have access to the ResourceDictionary used by your user interface, and while it is probably possible to find it through the container parameter, I really didn't feel like doing the work.

So, out comes Google and I start searching for alternative solutions, of which there were surprisingly few. The general response was how to use DataTemplateSelectors and there were a few decent articles on how you could move the selection code out of your apparently numerous selector classes. I wouldn't know, since I was just starting to write my first one, but I took their word for it anyway and kept looking.

After a few minutes I got bored of this and decided to try my hand at writing my own. One of my main aims was to be able to add new template selectors from within my WPF code rather than having to resort to writing code behind. One of the great features of WPF is the ability to register attached properties on objects you do not have the source to. This allows you to add functionality that wasn't there to start with and extend existing classes in creative new ways.

The result was a class which allowed me to add data templates, along with selection criteria, right from within my WPF code using an attached property.

DynamicDataTemplate Implementation

C#
/// <summary>
/// Provides a means to specify DataTemplates to be selected from within WPF code
/// </summary>
public class DynamicTemplateSelector : DataTemplateSelector
{
    /// <summary>
    /// Generic attached property specifying <see cref="Template"/>s
    /// used by the <see cref="DynamicTemplateSelector"/>
    /// </summary>
    /// <remarks>
    /// This attached property will allow you to set the templates you wish to be available whenever
    /// a control's TemplateSelector is set to an instance of <see cref="DynamicTemplateSelector"/>
    /// </remarks>
    public static readonly DependencyProperty TemplatesProperty = 
        DependencyProperty.RegisterAttached("Templates", typeof(TemplateCollection), typeof(DataTemplateSelector),
        new FrameworkPropertyMetadata(new TemplateCollection(), FrameworkPropertyMetadataOptions.Inherits));

                
    /// <summary>
    /// Gets the value of the <paramref name="element"/>'s attached <see cref="TemplatesProperty"/>
    /// </summary>
    /// <param name="element">The <see cref="UIElement"/> who's attached template's property you wish to retrieve</param>
    /// <returns>The templates used by the givem <paramref name="element"/>
    /// when using the <see cref="DynamicTemplateSelector"/></returns>
    public static TemplateCollection GetTemplates(UIElement element)
    {
        return (TemplateCollection)element.GetValue(TemplatesProperty);
    }

    /// <summary>
    /// Sets the value of the <paramref name="element"/>'s attached <see cref="TemplatesProperty"/>
    /// </summary>
    /// <param name="element">The element to set the property on</param>
    /// <param name="collection">The collection of <see cref="Template"/>s to apply to this element</param>
    public static void SetTemplates(UIElement element, TemplateCollection collection)
    {
        element.SetValue(TemplatesProperty, collection);
    }

    /// <summary>
    /// Overriden base method to allow the selection of the correct DataTemplate
    /// </summary>
    /// <param name="item">The item for which the template should be retrieved</param>
    /// <param name="container">The object containing the current item</param>
    /// <returns>The <see cref="DataTemplate"/> to use when rendering the <paramref name="item"/></returns>
    public override System.Windows.DataTemplate SelectTemplate(object item, System.Windows.DependencyObject container)
    {
        //This should ensure that the item we are getting is in fact capable of holding our property
        //before we attempt to retrieve it.
        if (!(container is UIElement))
            return base.SelectTemplate(item, container);

        //First, we gather all the templates associated with the current control through our dependency property
        TemplateCollection templates = GetTemplates(container as UIElement);
        if(templates == null || templates.Count == 0)
            base.SelectTemplate(item, container);

        //Then we go through them checking if any of them match our criteria
        foreach (var template in templates)
            //In this case, we are checking whether the type of the item
            //is the same as the type supported by our DataTemplate
            if (template.Value.IsInstanceOfType(item))
                //And if it is, then we return that DataTemplate
                return template.DataTemplate;
            
        //If all else fails, then we go back to using the default DataTemplate
        return base.SelectTemplate(item, container);
    }
}

/// <summary>
/// Holds a collection of <see cref="Template"/> items
/// for application as a control's DataTemplate.
/// </summary>
public class TemplateCollection : List<Template>
{

}

/// <summary>
/// Provides a link between a value and a <see cref="DataTemplate"/>
/// for the <see cref="DynamicTemplateSelector"/>
/// </summary>
/// <remarks>
/// In this case, our value is a <see cref="System.Type"/> which we are attempting to match
/// to a <see cref="DataTemplate"/>
/// </remarks>
public class Template : DependencyObject
{
    /// <summary>
    /// Provides the value used to match this <see cref="DataTemplate"/> to an item
    /// </summary>
    public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(Type), typeof(Template));

    /// <summary>
    /// Provides the <see cref="DataTemplate"/> used to render items matching the <see cref="Value"/>
    /// </summary>
    public static readonly DependencyProperty DataTemplateProperty = 
       DependencyProperty.Register("DataTemplate", typeof(DataTemplate), typeof(Template));

    /// <summary>
    /// Gets or Sets the value used to match this <see cref="DataTemplate"/> to an item
    /// </summary>
    public Type Value
    { get { return (Type)GetValue(ValueProperty); } set { SetValue(ValueProperty, value); } }

    /// <summary>
    /// Gets or Sets the <see cref="DataTemplate"/> used to render items matching the <see cref="Value"/>
    /// </summary>
    public DataTemplate DataTemplate
    { get { return (DataTemplate)GetValue(DataTemplateProperty); } set { SetValue(DataTemplateProperty, value); } }
}

Using the Code

XML
<local:DynamicTemplateSelector x:Key="MyTemplateSelector" />

<DataTemplate x:Key="StringTemplate">
    <TextBlock>
        <Run Text="String: " />
        <Run Text="{Binding}" />
    </TextBlock>
</DataTemplate>

<DataTemplate x:Key="Int32Template">
    <TextBlock>
        <Run Text="Int32: " />
        <Run Text="{Binding}" />
    </TextBlock>
</DataTemplate>

<Style x:Key="MyListStyle" TargetType="ListView">
    <Setter Property="ItemTemplateSelector" Value="{StaticResource MyTemplateSelector}"/>
    <Setter Property="local:DynamicTemplateSelector.Templates">
        <Setter.Value>
            <local:Templates>
                <local:Template Value={x:Type String} DataTemplate={StaticResource StringTemplate}/>
                <local:Template Value={x:Type Int32} DataTemplate={StaticResource Int32Template}/>
            </local:Templates>
        </Setter.Value>
    </Setter>
</Style>

It may not look it just yet, but when you start adding lots of different types, or templates, this quickly becomes a lot easier to manage; allowing you to add templates and types without needing to change any back end code.

Points of Interest

As I said, my main reason for writing this code was to maintain an MVVM structure within the project I was working on. I only recently started writing code to match the MVVM paradigm and I can truly say that it has been one of the best things I've done since I moved away from Java a few years ago. If you haven't yet, take a look at the truly exceptional Caliburn.Micro and put the time into learning how to use it.

Other than that, I'd also recommend learning how to make use of attached Dependency Properties, they are useful for all manner of things from adding watermarks to your text boxes (something I have only seen one person do properly so far in WPF) to allowing Caliburn.Micro to hide and unhide a window using a binding without having to write your own WindowManager.

History

  • 06/07/2012 - Initial release.
  • 09/07/2012 - Fixed some issues with inheritance of DependencyProperty and added a collection to make adding values easier.

License

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

Share

About the Author

Sierra Softworks
Software Developer Sierra Softworks
South Africa South Africa
Sierra Softworks is a small South African company specializing in writing high performance desktop and mobile applications. Our specialty lies in network protocol design and implementation and we have been making a name for ourselves as a top tier provider of efficient, secure and stable network protocols for all manner of applications. We pride ourselves on our prompt customer support and knowledgable service.
Group type: Organisation (No members)



Comments and Discussions

 
SuggestionOverkill Pin
Henry.Ayoola12-Nov-15 23:03
MemberHenry.Ayoola12-Nov-15 23:03 
GeneralRe: Overkill Pin
Janos Veres12-Aug-17 5:42
MemberJanos Veres12-Aug-17 5:42 
QuestionMy vote of 4. Nice article Pin
Amit Gupta AG30-Dec-14 3:33
MemberAmit Gupta AG30-Dec-14 3:33 
BugStruggling to make this work in a 'simple' ContentControl Pin
mwpowellhtx19-Dec-14 4:37
Membermwpowellhtx19-Dec-14 4:37 
BugCould not make it work attached properties on non DependencyObjects Pin
mwpowellhtx18-Dec-14 12:59
Membermwpowellhtx18-Dec-14 12:59 
GeneralRe: Could not make it work attached properties on non DependencyObjects Pin
mwpowellhtx18-Dec-14 16:58
Membermwpowellhtx18-Dec-14 16:58 
GeneralRe: Could not make it work attached properties on non DependencyObjects Pin
mwpowellhtx19-Dec-14 2:34
Membermwpowellhtx19-Dec-14 2:34 
GeneralRe: Could not make it work attached properties on non DependencyObjects Pin
gb11514-Apr-15 3:17
Membergb11514-Apr-15 3:17 
QuestionDynamic datatemplate for LongListSelector in Windows Phone 8 Pin
obs12314-May-14 0:36
professionalobs12314-May-14 0:36 
QuestionThis looks awesome. Pin
bgabric22-May-13 8:07
Memberbgabric22-May-13 8:07 
GeneralMy vote of 5 Pin
Todd Pichler9-Apr-13 19:28
MemberTodd Pichler9-Apr-13 19:28 
Very clean, great description
GeneralMy vote of 5 Pin
Pete O'Hanlon9-Jul-12 5:52
mvePete O'Hanlon9-Jul-12 5:52 
QuestionCool, but there is an MVVM friendly way of using an ItemTemplateSelector Pin
Pete O'Hanlon9-Jul-12 5:42
mvePete O'Hanlon9-Jul-12 5:42 
AnswerRe: Cool, but there is an MVVM friendly way of using an ItemTemplateSelector Pin
Sierra Softworks9-Jul-12 5:47
MemberSierra Softworks9-Jul-12 5:47 
GeneralRe: Cool, but there is an MVVM friendly way of using an ItemTemplateSelector Pin
Pete O'Hanlon9-Jul-12 5:52
mvePete O'Hanlon9-Jul-12 5:52 
GeneralRe: Cool, but there is an MVVM friendly way of using an ItemTemplateSelector Pin
Sierra Softworks9-Jul-12 6:05
MemberSierra Softworks9-Jul-12 6:05 
QuestionI recall Josh Smith writing something like this a while back Pin
Sacha Barber9-Jul-12 4:57
mvaSacha Barber9-Jul-12 4:57 
AnswerRe: I recall Josh Smith writing something like this a while back Pin
Sierra Softworks9-Jul-12 5:04
MemberSierra Softworks9-Jul-12 5:04 

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

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