[UWP]ItemsControl and ItemTemplateSelector in Windows 10 UWP app RRS feed

  • Question

  • I think this should work and cannot figure out why. I think it is actually a bug.  Basically I want to use an ItemsControl (because I just want to list some data, I don't want selection) instead of a ListView control.  If I use a ListView, it works, but has selection which I dont want.  It also works if I specific an ItemTemplate.  Is just doesnt work with ItemTemplateSelector.  This is a universal windows platform app.

    Here are my resources:

        <DataTemplate x:Key="SentMessageDataTemplate">
            <TextBlock Text="Sent" />
        <DataTemplate x:Key="ReceivedMessageDataTemplate">
            <TextBlock Text="Recieved" />
        <services:MessageDataTemplateSelector x:Key="MessageDataTemplateSelector" ReceivedTemplate="{StaticResource ReceivedMessageDataTemplate}" SentTemplate="{StaticResource SentMessageDataTemplate}"></services:MessageDataTemplateSelector>

    Here is my ItemsControl:

    <ItemsControl ItemsSource="{Binding Messages}" ItemTemplateSelector="{StaticResource MessageDataTemplateSelector}" />

    Here is my DataTemplateSelector:

    public class MessageDataTemplateSelector : DataTemplateSelector
        public DataTemplate SentTemplate
        public DataTemplate ReceivedTemplate
        protected override DataTemplate SelectTemplateCore(object item)
            var message = item as MessageViewModel;
            if (message == null)
                return this.SentTemplate;
            return message.Sent ? this.SentTemplate : this.ReceivedTemplate;

    Instead of displaying either of my templates it just displays my ViewModel type name (so basically ToString).

    However if I switch it from ItemsControl to ListView, it works fine. 

    Any suggestions?

    Friday, January 22, 2016 8:44 AM


All replies

  • What happens if you override the other SelectTemplate(Object, DependencyObject) method?  Perhaps it is being called instead of the one you're overriding.

    Darin R.

    Friday, January 22, 2016 6:53 PM
  • Hi Cleve,

    I have tested @Darin's solution, it works fine. For the ItemsControl we need to use the SelectTemplate(Object, DependencyObject) method, but for the ListView which is inherited from the ItemsControl, we need to use the SelectTemplateCore(object item) method.

    Best Regards,
    Amy Peng

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Saturday, January 23, 2016 4:42 AM