C# WPF 基于 DataTrigger 更改 ContentControl 内容不起作用
我正在尝试更改基于 DataTrigger 的内容控件的内容,但没有成功,它会检查绑定以查看它是否与类型匹配,以及它是否确实更改了 contentcontrols
显示的内容.
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.
简而言之,如果 Source 是某种类型,我希望这个 ContentControl 的 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>
我已经测试过转换器:IsAssociation
被正确命中,并且是正确的值,但内容没有更改为 stackpanel
中定义的数据触发器
.我已经使用 Visual Studios 的 Visual Tree 确认了这一点,它仍然链接到旧内容.
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.
一个直接设置Content
like
<ContentControl>
<Content>
...
</Content>
</ContentControl>
具有更高的 值优先 比由样式触发器中的 Setter 设置的值.因此,您的 DataTrigger 将被忽略.
has higher value precedence than a value set by a Setter in a Style Trigger. Your DataTrigger is hence ignored.
不是直接设置Content,而是将初始值移动到另一个Setter:
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>