Ilan Keshet
Ilan Keshet

Reputation: 594

C# WPF Changing ContentControl Content based on DataTrigger not working

I am trying, with no luck, to change the content of a content control based upon a DataTrigger, that checks a binding to see if it matches a type, and if it does change the contentcontrols displayed content.

In short, I want this ContentControl's Content to change if the Source is a certain type. I have attempted to do this using Styles / DataTriggers

<ContentControl>
  <ContentControl.Content>
    <TextBlock Name="CollectionControlTextBox1" 
         Tag="." 
         PreviewMouseDown="CollectionControlDefaultDockPanel_MouseButtonDown"
         PreviewMouseUp="CollectionControlTextBox1_PreviewMouseUp"
         MouseMove="CollectionControlDefaultDockPanel_MouseMove"
         Drop="CollectionControlDefaultDockPanel_Drop"
         Foreground="{Binding Meta.ColorBrush, Mode=OneWay, TargetNullValue={StaticResource TextBrush}, FallbackValue={StaticResource TextBrush}}"
         AllowDrop="True"
         MinHeight="20" 
         Padding="5 2 0 0"
         KeyboardNavigation.IsTabStop="False"
         Focusable="True"
         TextTrimming="CharacterEllipsis"
         ContextMenu="{Binding ParentControl.MemberContextMenu, Mode=OneWay}"
         Text="{Binding DisplayName, Mode=OneWay}">
      <TextBlock.CommandBindings>
        <CommandBinding Command="{x:Static iugo:EditorCommands.Metadata}" Executed="Metadata" />
      </TextBlock.CommandBindings>
    </TextBlock>
  </ContentControl.Content>
  <ContentControl.Style>
    <Style TargetType="ContentControl">
      <Style.Triggers>
        <DataTrigger Binding="{Binding Source, Converter={StaticResource IsAssociation}}" Value="True">
          <Setter Property="Content">
            <Setter.Value>
              <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding IdEntry.Id}" ToolTip="{Binding IdEntry.Entry.Name}" FontSize="10" Foreground="{StaticResource DisabledTextBrush}" HorizontalAlignment="Center" Margin="0, 3, 0, 0"/>
                <TextBlock Text="{Binding IdEntry.Entry.Name}" ToolTip="{Binding IdEntry.Entry.Name}" FontSize="13" Margin="5, 0, 0, 0"/>
              </StackPanel>
            </Setter.Value>
          </Setter>
        </DataTrigger>
      </Style.Triggers>
    </Style>
  </ContentControl.Style>
</ContentControl>

I have tested that the converter: IsAssociation is correctly being hit, and is correctly the right value, but the Content does not change to a stackpanel as defined in the DataTrigger. I have confirmed this by using Visual Studios' Visual Tree, and it still links to the old content.

Upvotes: 1

Views: 1381

Answers (1)

Clemens
Clemens

Reputation: 128013

A directly set Content like

<ContentControl>
    <Content>
        ...
    </Content>
</ContentControl>

has higher value precedence than a value set by a Setter in a Style Trigger. Your DataTrigger is hence ignored.

Instead of directly setting the Content, move the initial value to another Setter:

<ContentControl>
    <ContentControl.Style>
        <Style TargetType="ContentControl">
            <Setter Property="Content">
                <Setter.Value>
                    <TextBlock ...>
                        ...
                    </TextBlock>
                </Setter.Value>
             </Setter>
             <Style.Triggers>
                 <DataTrigger ...>
                     <Setter Property="Content">
                         <Setter.Value>
                             <StackPanel Orientation="Horizontal">
                                 ...
                             </StackPanel>
                         </Setter.Value>
                     </Setter>
                 </DataTrigger>
             </Style.Triggers>
         </Style>
    </ContentControl.Style>
</ContentControl>

Upvotes: 3

Related Questions