C# WPF 更改基于 DataTrigger 的 ContentControl 内容不起作用

C# WPF Changing ContentControl Content based on DataTrigger not working

我正在尝试更改基于 DataTrigger 的内容控件的内容,但没有成功,它会检查绑定以查看它是否与类型匹配,如果它确实更改了显示的 contentcontrols内容.

简而言之,如果 Source 是某种类型,我希望此 ContentControl 的 Content 发生变化。我尝试使用 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>

我已经测试转换器:IsAssociation 被正确命中,并且是正确的值,但内容不会更改为 [=14= 中定义的 stackpanel ].我已经使用 Visual Studios 的 Visual Tree 确认了这一点,它仍然链接到旧内容。

一个直接设置Content喜欢

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

比样式触发器中 Setter 设置的值高 value precedence。因此,您的 DataTrigger 将被忽略。

不是直接设置内容,而是将初始值移动到另一个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>