Search code examples
wpfdata-bindingdatatemplatedatatriggergroupstyle

Setting different styles for Dual level grouping with DataTrigger


I have a dual level grouping and thought I could define different styles with DataTriggers.

Thinking that GroupStyles.HeaderTemplate would bind to CollectionViewGroup I tried DataBinding to IsBottomLevel property.

<GroupStyle>
     <GroupStyle.HeaderTemplate>
          <DataTemplate>
              <TextBlock x:Name="GroupName"
                         Text="{Binding Path=Name}"
                         Foreground="Red" />
                   <DataTemplate.Triggers>
                         <DataTrigger Binding="IsBottomLevel" Value="True" >
                              <Setter TargetName="GroupName" Property="Foreground" Value="Blue" />
                         </DataTrigger>
                    </DataTemplate.Triggers>
            </DataTemplate>
       </GroupStyle.HeaderTemplate>
  </GroupStyle>

Can I get this to work somehow?


Solution

  • Define your trigger in the Style of the TextBlock itself, TargetName normally is for ControlTemplates, then you can just drop that.

    This is not a binding:

    Binding="IsBottomLevel"
    

    You should replace it with the following of course:

    Binding="{Binding IsBottomLevel}"
    

    <TextBlock Text="{Binding Name}">
        <TextBlock.Style>
            <Style TargetType="{x:Type TextBlock}">
                <Setter Property="Foreground" Value="Red" />
                <Style.Triggers>
                    <DataTrigger Binding="{Binding IsBottomLevel}" Value="True">
                        <Setter Property="Foreground" Value="Blue" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </TextBlock.Style>
    </TextBlock>
    

    If you set the red Foreground directly in the TextBlock declaration the trigger will have no effect due to precedence.