Click here to Skip to main content
13,258,479 members (52,338 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


3 bookmarked
Posted 15 May 2014

DataTemplates in WinRT

, 15 May 2014
Rate this:
Please Sign up or sign in to vote.
DataTemplates in WinRT

While I enjoy the simpler interfaces in WinRT, I’ve been spoiled by the power of WPF. One thing I missed recently was the automatic selection of a DataTemplate based on the DataType property; this property isn’t there in WinRT.

The solutions I’ve seen use a custom DataTemplateSelector with various properties for each type. This certainly works but it felt a bit repetitive. Instead, the solution I came up with searches the resources for an item with the same key as the type name and returns that. Here’s the class:

namespace AutoDataTemplate
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Media;

    public sealed class AutoDataTemplateSelector : DataTemplateSelector
        protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
            if (item == null)
                return null;

            object key = item.GetType().Name;
            return FindResource(key, container as FrameworkElement) as DataTemplate;

        private static object FindResource(object key, FrameworkElement element)
            if (element == null)
                return null;

            object value;
            if (element.Resources.TryGetValue(key, out value))
                return value;

            return FindResource(key, VisualTreeHelper.GetParent(element) as FrameworkElement);

Using this class would be something like this:

<Page x:Class="AutoDataTemplate.MainPage"




        <adt:AutoDataTemplateSelector x:Key="AutoSelector" />
        <DataTemplate x:Key="ClassType1">
            <TextBlock Foreground="CornflowerBlue"

                       Text="{Binding Value}" />

        <DataTemplate x:Key="ClassType2">
            <TextBlock Foreground="Firebrick"

                       Text="{Binding DifferentValue}" />

    <ListView ItemsSource="{Binding}"

              ItemTemplateSelector="{StaticResource AutoSelector}"

              Padding="40" />

Of course, there are limitations with this approach, mainly that namespaces cannot be used (i.e., you can’t have the key as local:MyType). This means it is not possible to auto select the template if two types have the same name but live in different namespaces – for my scenario, this wasn’t a problem.


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


About the Author

Samuel Cragg
United Kingdom United Kingdom
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
angus1511-Dec-14 19:46
memberangus1511-Dec-14 19:46 
Question[My vote of 2] Could you please explain in detail... Pin
Naz_Firdouse2-Jun-14 4:44
memberNaz_Firdouse2-Jun-14 4:44 
AnswerRe: [My vote of 2] Could you please explain in detail... Pin
Samuel Cragg2-Jun-14 20:46
memberSamuel Cragg2-Jun-14 20:46 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171114.1 | Last Updated 15 May 2014
Article Copyright 2014 by Samuel Cragg
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid