Use same ViewModel,change its data template

4 posts, 0 answers
  1. Tomwe
    Tomwe avatar
    8 posts
    Member since:
    Dec 2011

    Posted 15 Dec 2012 Link to this post


    I need to change the selected data template based on the state of the view model(the binding)
    Here is my Xaml
    (the content always point to "MainViewModel")



    ContentTemplateSelector="{StaticResource pageTemplateSelector}"

    Transition="{Binding Transition}"




    Thanks in advance
  2. Ivo
    Ivo avatar
    390 posts

    Posted 18 Dec 2012 Link to this post

    Hello Tomwe,

    The SelectTemplate method of the ContentTemplateSelector is called every time when the Content is changed. I believe that you won't be able to change the ContentTemplate using the ContentTemplateSelector without changing the Content property. I woud suggest you to change the approach you use to achieve the desired behavior. Can you give us more details on your scenario so we can assist you and give you guidelines on this?

    Kind regards,
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. DevCraft banner
  4. Tomwe
    Tomwe avatar
    8 posts
    Member since:
    Dec 2011

    Posted 26 Jan 2013 Link to this post

    The solution was to bind the Content to the state of the main view model
    where states represented as other  view models
  5. Ramon
    Ramon avatar
    11 posts
    Member since:
    Aug 2012

    Posted 20 Mar 2014 in reply to Tomwe Link to this post

    I used an enum for state changing
    here is a sample

    <DataTemplate x:Key="ListUserDataTemplate" >
        <userView:ListuserData />
    <DataTemplate x:Key="NonerDataTemplate">
        <Grid >
    <DataTemplate x:Key="MapUserDataTemplate">
    <Style x:Key="ManWindowHolderStyle" TargetType="{x:Type ContentControl}">
        <Setter Property="Template">
                <ControlTemplate TargetType="{x:Type ContentControl}">
                    <telerik:RadTransitionControl Content="{Binding}" Transition="PerspectiveRotation" Duration="00:00:00.7" x:Name="ContentWindow" />
                        <DataTrigger Binding="{Binding AppStateHelper.MainWindowType}" Value="ListUserType">
                            <Setter Property="ContentTemplate" Value="{StaticResource ListUserDataTemplate}" TargetName="ContentWindow" />
                        <DataTrigger Binding="{Binding AppStateHelper.MainWindowType}" Value="MapUserType">
                            <Setter Property="ContentTemplate" Value="{StaticResource MapUserDataTemplate}" TargetName="ContentWindow"/>
                        <DataTrigger Binding="{Binding AppStateHelper.MainWindowType}" Value="None">
                            <Setter Property="ContentTemplate" Value="{StaticResource NonerDataTemplate}" TargetName="ContentWindow"/>

    and i used a  ContentControl with the style applyed bound to the enum, you can use instead a viewmodel

    <ContentControl Content="{Binding AppStateHelper.MainWindowType}"
                               Style="{StaticResource ManWindowHolderStyle}" />
Back to Top