更改ContentPresenter的DataTemplate

时间:2014-04-08 14:40:00

标签: c# windows-phone-8 mvvm contentpresenter datatemplateselector

我有一个弹出窗口,我想根据点击的各种按钮显示不同的东西。为此,我在此ContentPresenter中添加了一个ContentPresenter nad,我有一个TemplateSelector。我的问题是,据我所知,它只检查我的popUp第一次运行时使用哪个模板,然后使用此模板。有没有办法让代码更改要使用的模板?

到目前为止我的代码是(xaml):

  <Popup IsOpen="{Binding IsOpen}" Height="{Binding Height}" Width="{Binding Width}">

        <Grid>
            <ContentPresenter x:Name="CP" Loaded="CP_Loaded">
                    <ViewModel:PopUpTemplateSelector x:Name="PUT" Content="{Binding}">
                        <ViewModel:PopUpTemplateSelector.View1>
                            <DataTemplate>
                                <View:View1/>
                            </DataTemplate>
                        </ViewModel:PopUpTemplateSelector.View1>
                        <ViewModel:PopUpTemplateSelector.View2>
                            <DataTemplate>
                                <View:View2/>
                            </DataTemplate>
                        </ViewModel:PopUpTemplateSelector.View2>
                        <ViewModel:PopUpTemplateSelector.View3>
                            <DataTemplate>
                                <View:View3/>
                            </DataTemplate>
                        </ViewModel:PopUpTemplateSelector.View3>
                        <ViewModel:PopUpTemplateSelector.View4>
                            <DataTemplate>
                                <View:View4/>
                            </DataTemplate>
                        </ViewModel:PopUpTemplateSelector.View4>
                        <ViewModel:PopUpTemplateSelector.View5>
                            <DataTemplate>
                                <Design:View5/>
                            </DataTemplate>
                        </ViewModel:PopUpTemplateSelector.View5>
                    </ViewModel:PopUpTemplateSelector>
            </ContentPresenter>
        </Grid>
    </Popup>

我的popUpTemplateSelector(C#)是

    public class PopUpTemplateSelector : DataTemplateSelector
{
    public DataTemplate View1{ get; set; }
    public DataTemplate View2 { get; set; }
    public DataTemplate View3 { get; set; }
    public DataTemplate View4 { get; set; }
    public DataTemplate View5 { get; set; }


    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {

        PopUpViewModel Pop = item as PopUpViewModel;

        if(Pop.TemplateToUse == "View1")
        {
            return View1;
        }
        else if(Pop.TemplateToUse == "View2")
        {
            return View2;
        }
        else if(Pop.TemplateToUse.Equals("View3"))
        {
            return View3;
        }
        else if (Pop.TemplateToUse.Equals("View4"))
        {
            return View4;
        }
        else if(Pop.TemplateToUse.Equals("View5"))
        {
            return View5;
        }

        return null;
    }   

}

1 个答案:

答案 0 :(得分:1)

我建议您使用绑定到ViewModel上的TemplateToUse属性的DataTriggers来更新ContentTemplate。并使用ContentControl代替ContentPresenter

 <Popup IsOpen="{Binding IsOpen}" Height="{Binding Height}" Width="{Binding Width}">
    <Grid>
        <Grid.Resources>
            <DataTemplate x:Key="View1Template">
                <View:View1/>
            </DataTemplate>
            <DataTemplate x:Key="View2Template">
                <View:View2/>
            </DataTemplate>
            <DataTemplate x:Key="View3Template">
                <View:View3/>
            </DataTemplate>
            <DataTemplate x:Key="View4Template">
                <View:View4/>
            </DataTemplate>
            <DataTemplate x:Key="View5Template">
                <Design:View5/>
            </DataTemplate>

            <Style x:Key="ContentStyle" TargetType="{x:Type ContentControl}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Path=TemplateToUse}" Value="View1">
                        <Setter Property="ContentTemplate" Value="{StaticResource View1Template}" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Path=TemplateToUse}" Value="View2">
                        <Setter Property="ContentTemplate" Value="{StaticResource View2Template}" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Path=TemplateToUse}" Value="View3">
                        <Setter Property="ContentTemplate" Value="{StaticResource View3Template}" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Path=TemplateToUse}" Value="View4">
                        <Setter Property="ContentTemplate" Value="{StaticResource View4Template}" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Path=TemplateToUse}" Value="View5">
                        <Setter Property="ContentTemplate" Value="{StaticResource View5Template}" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
       </Grid.Resources>

       <ContentControl x:Name="CP" Loaded="CP_Loaded" Style="{StaticResource ContentStyle}" Content="{Binding}" />
    </Grid>
</Popup>