How to use template selector within a ContentPresenter in Windows 8.1

烂漫一生 提交于 2019-12-23 02:31:49

问题


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.


回答1:


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. :)




回答2:


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



来源:https://stackoverflow.com/questions/28102668/how-to-use-template-selector-within-a-contentpresenter-in-windows-8-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!