Unable to set ContentTemplate via DataTrigger Unable to set ContentTemplate via DataTrigger wpf wpf

Unable to set ContentTemplate via DataTrigger


I know the OP has no use for this answer anymore but I thought I'd answer it anyway in case anyone else comes along with the same problem

The only problem with the Xaml in the question is that the ContentTemplate is set explicilty on the ContentControl and not in the Style and this overrides the Trigger. Setting it in the Style instead fixes the problem

<ContentControl Content="{Binding BoolProperty, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type src:Window1}}}">    <ContentControl.Resources>        <DataTemplate x:Key="Template2">            <CheckBox IsChecked="{Binding BoolProperty, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type src:Window1}}}" Content="Template 2"/>        </DataTemplate>    </ContentControl.Resources>    <ContentControl.Style>        <Style TargetType="ContentControl">            <Setter Property="ContentTemplate">                <Setter.Value>                    <DataTemplate>                        <CheckBox IsChecked="{Binding BoolProperty, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type src:Window1}}}" Content="Template 1"/>                    </DataTemplate>                </Setter.Value>            </Setter>            <Style.Triggers>                <DataTrigger Binding="{Binding BoolProperty, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type src:Window1}}}" Value="True">                    <Setter Property="ContentTemplate" Value="{StaticResource Template2}"/>                </DataTrigger>            </Style.Triggers>        </Style>    </ContentControl.Style></ContentControl>


Here's something that works for me:

<ContentControl Content="{Binding SomeBool}">  <ContentControl.Resources>    <DataTemplate x:Key="PinkTemplate">      <TextBlock Text="{Binding}" Background="Pink" />    </DataTemplate>    <DataTemplate x:Key="LimeTemplate">      <TextBlock Text="{Binding}" Background="Lime" />    </DataTemplate>  </ContentControl.Resources>  <ContentControl.ContentTemplate>    <DataTemplate>      <ContentControl Name="cc"                      Content="{Binding}"                      ContentTemplate="{StaticResource PinkTemplate}" />      <DataTemplate.Triggers>        <DataTrigger Binding="{Binding}" Value="True">          <Setter TargetName="cc"                   Property="ContentTemplate"                  Value="{StaticResource LimeTemplate}" />        </DataTrigger>      </DataTemplate.Triggers>    </DataTemplate>  </ContentControl.ContentTemplate></ContentControl>

Note that my DataTemplate is another ContentControl, which allows my DataTemplate.Triggers to operate on the ContentTemplate of that (nested) ContentControl.