1

I'm having the same problem as described here : ContentTemplateSelector is only called one time showing always the same datatemplate

I've attempted the same solution as Simon Weaver suggests (although his answer is somewhat abbreviated so i'm guessing it looks like the following):

<ContentControl >
   <Style>
     <Style.Triggers>
    <DataTrigger Binding="{Binding Path=IsSelected}" Value="true">
    <Setter Value="{StaticResource SelectedDataTemplate}" Property="ContentControl.ContentTemplate"></Setter>
     </DataTrigger>
<DataTrigger Binding="{Binding Path=IsSelected}" Value="false">
     <Setter Value="{StaticResource UnSelectedDataTemplate}"  Property="ContentControl.ContentTemplate">
      </Setter>
     </DataTrigger>
     </Style.Triggers>
     </Style>
</ContentControl>

However when i run this, i just get 'System.Windows.Style' displayed in my content control. As an aside, i've (sort of) got it working using an overridden DataTemplateSelector class, but the problem there is that the selector only gets evaluated on start up. I need this switching behaviour based on the data bound IsSelected property - which i was hoping the above snippet achieves. BTW the actual data templates themselves just contain UI stuff - no data triggers etc so i'm not including them in my post.

Community
  • 1
  • 1
Auburg
  • 155
  • 1
  • 2
  • 6
  • ok - i fixed it by wrapping much of the above in its own style and then applying the style to a content control – Auburg Aug 22 '11 at 12:39

1 Answers1

3

Your ContentControl is setting it's Content to the Style since you are missing your ContentControl.Style tag.

<ContentControl>
    <ContentControl.Style>
        <Style TargetType="{x:Type ContentControl}">
            <Setter Value="{StaticResource UnSelectedDataTemplate}" Property="ContentTemplate" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=IsSelected}" Value="True">
                    <Setter Value="{StaticResource SelectedDataTemplate}" Property="ContentTemplate" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ContentContro.Style>
</ContentControl>
Rachel
  • 130,264
  • 66
  • 304
  • 490
  • great, thanks for that. This is a more elegant solution than having to create a separate style. – Auburg Aug 22 '11 at 14:09