1

I have a popup, where I want to display different things depending on various buttons which are clicked. To do this I've added a ContentPresenter nad in this ContentPresenter I've got an TemplateSelector. My problem is that as far as I can see it only checks which template to use the first time my popUp is run and uses this template from then on. Is there a way to get the code to change the template to use?

The code I've got so far is (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>

and my popUpTemplateSelector(C#) is

    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;
    }   

}
JonasN89
  • 1,386
  • 2
  • 11
  • 23

1 Answers1

1

I would suggest you use DataTriggers bound to TemplateToUse property on the ViewModel to update ContentTemplate. And also use ContentControl instead of 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>
vadim
  • 176
  • 6
  • Just to make sure I understand this right, so whenever I change the templateToUse in my code, the PopUpTemplateSelector, will be run? – JonasN89 Apr 09 '14 at 14:54
  • No, whenever you change TemplateToUse property (assuming that your viewmodel implements `INotifyPropertyChanged` and you raise `PropertyChanged` event when assigning to that property) one of DataTriggers will update the `ContentTemplate` property of ContentControl. PopUpTemplateSelector is not involved here – vadim Apr 09 '14 at 21:34
  • I was slowly working through you suggestion, but the Style.Trigger is not supported for Windows-phone, any suggestion on what to use instead? – JonasN89 Apr 11 '14 at 07:37
  • Then i'd suggest you to keep using your TemplateSelector but modifying it to update `ContentTemplate` property when `TemplateToUse` property changes on the view model. See an example here - http://stackoverflow.com/questions/13467018/how-to-trigger-datatemplate-selector-in-windows-phone BTW msdn article says style triggers are supported in Win Phone 8 - http://msdn.microsoft.com/en-us/library/system.windows.style.triggers(v=vs.110).aspx – vadim Apr 11 '14 at 09:59