代码之家  ›  专栏  ›  技术社区  ›  Nicolas

文本块文本的整数值0上的DataTrigger不工作[重复]

  •  1
  • Nicolas  · 技术社区  · 6 年前

    这个问题已经有了答案:

    我有一个 TextBlock 显示视图模型中可用的条目数。现在我试着用 FontWeight Bold 如果有超过零个条目。

    为了简单(避免 ValueConverter )我颠倒了逻辑并应用了默认值 字体权重 属于 大胆的 尝试重写此 字体权重 通过 DataTrigger ,如果没有条目。

    但恐怕这不起作用,计数一直显示为粗体:

    <TextBlock Text="{Binding Entries.Count, StringFormat=' [{0}]'}" FontWeight="Bold">
        <TextBlock.Style>
            <Style TargetType="{x:Type TextBlock}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding SharedNodeNames.Count}" Value="0">
                        <Setter Property="FontWeight" Value="Regular" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </TextBlock.Style>
    </TextBlock>
    

    有什么问题吗?

    (出于测试目的,我移除了 StringFormat 中的一部分 控件 Text 绑定,但这没有区别)

    1 回复  |  直到 6 年前
        1
  •  0
  •   mm8    6 年前

    设置的默认值 FontWeight A中的属性 Style 设定者:

    <TextBlock Text="{Binding Entries.Count, StringFormat=' [{0}]'}">
        <TextBlock.Style>
            <Style TargetType="{x:Type TextBlock}">
                <Setter Property="FontWeight" Value="Bold" />
                <Style.Triggers>
                    <DataTrigger Binding="{Binding SharedNodeNames.Count}" Value="0">
                        <Setter Property="FontWeight" Value="Regular" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </TextBlock.Style>
    </TextBlock>
    

    局部值优先于样式设置的值: https://docs.microsoft.com/en-us/dotnet/framework/wpf/advanced/dependency-property-value-precedence