How to use template selector within a ContentPresenter in Windows 8.1

I have a Windows 8.1 application. I have a requirement of selecting different templates based on a certain value. For this purpose I'm using ContentPresenter in the xaml with a Static Resource TemplateSelector.

Here's my datatemplates and templateselector in xaml resources

    <DataTemplate x:Key="template1">
        <TextBox Text="Temp 1" />
    </DataTemplate>

    <DataTemplate x:Key="template2">
        <TextBox Text="Temp 2" />
    </DataTemplate>

    <DataTemplate x:Key="template3">
        <TextBox Text="Temp 3" />
    </DataTemplate>

    <template:BalanceTypesTemplateSelector x:Key="MySelector"
                                           Template1="{StaticResource template1}"
                                           Template2="{StaticResource template2}"
                                           Template3="{StaticResource template3}" />

Here's my ContentPresenter XAML

<ContentPresenter ContentTemplateSelector="{StaticResource MySelector}"
                                                Content="{Binding MyData}" />

Here's my Template Selector Code

public class BalanceTypesTemplateSelector : DataTemplateSelector
{
    public DataTemplate Template1 { get; set; }
    public DataTemplate Template2 { get; set; }
    public DataTemplate Template3 { get; set; }

    protected override DataTemplate SelectTemplateCore(object item)
    {
            var type = item.ToString();
            switch (type)
            {
                case "t1":
                    return Template1;
                case "t2":
                    return Template1;
                case "t3":
                    return Template3;
                default:
                    throw new NotSupportedException();
            }
        }

        return null;
    }

}

But it's not hitting the templateselector code at all. The string that is bound is directly displayed on the display when I run the app.

I would be glad if some one point me in the right direction. Thanks in Advance.

Answers


Basically, you're only overriding one of the SelectTemplateCore overloads.

From the DataTemplateSelector docs:

To define an effective DataTemplateSelector subclass, provide implementations for SelectTemplateCore(Object) and SelectTemplateCore(Object, DependencyObject)

Once you provide an implementation for SelectTemplateCore(Object, DependencyObject), it will get invoked.

I tried to do it, but there was another problem I encountered - the object is always null (and not the Content/DataContext of the ContentPresenter).

I asked Google why is that and found this discussion. From it:

The ContentControl and ContentPresenter appear to be broken in Windows RT when used with a ContentTemplateSelector property bound to a view model. The 'object' parameter to the template selector is always null.

There's also a workaround for this problem at the end of that discussion.

Hope this helps. :)


Using ContentControl instead of ContentPresenter is working for me. Thanks @KaiBrummund for his comment on my question.


Need Your Help

Delete method in WCF

c# wcf linq web-services rest

I'm looking for a LINQ delete method for this:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.