【问题标题】:StringFormat in GroupBox header binding doesn't seem to workGroupBox 标头绑定中的 StringFormat 似乎不起作用
【发布时间】:2016-07-11 20:46:24
【问题描述】:

我正在尝试将 aGroupBox 的标题文本绑定到一个属性并使用 StringFormat 显示它。

第一部分有效,它按预期返回文本。但我希望最终文本被格式化。例如,当我返回cm 时,我希望它显示为Foundation Height (cm),但下面的代码只显示cm

<GroupBox Header="{Binding CurrentTargetUnit, 
    Converter={StaticResource QuantityToTextConverter},
    ConverterParameter={x:Static enumerations:Quantity.Length},
    StringFormat='Foundation Height ({0})'}">
</GroupBox>

【问题讨论】:

  • @FirstStep 还是一样。

标签: c# xaml bind string-formatting ivalueconverter


【解决方案1】:

不带引号试试

....
StringFormat=Foundation Height ({0})}">

更新

由于您已对标头使用绑定,因此您应该改用HeaderStringFormat

....
HeaderStringFormat=Foundation Height ({0})}">

阅读documentation了解更多信息

【讨论】:

  • 很奇怪!似乎对于GroupBoxStringFormat 不起作用。我试过HeaderStringFormat,它现在可以工作了。
【解决方案2】:

我遇到了同样的问题。我找到了我的答案here

<GroupBox>
    <GroupBox.Header>
        <TextBlock Text="{Binding CurrentTargetUnit, 
                                  Converter={StaticResource QuantityToTextConverter},
                                  ConverterParameter={x:Static enumerations:Quantity.Length},
                                  StringFormat=Foundation Height ({0})}">
    </GroupBox.Header>
</GroupBox>

您还需要从 StringFormat 中删除引号。此外,在上面的链接中,它显示将“{}”放在 StringFormat 中的任何内容之前。如果我没记错的话,你只需要占位符是第一个: StringFormat={}{0:C}

允许 StringFormat 绑定的元素数量有限,TextBlock 是其中之一,而 GroupBox 标题则不允许。

【讨论】:

  • 给这个人一块饼干!此外,仅当StringFormat 以占位符(如{0})开头时,{} 才是强制性的。
【解决方案3】:

试试这个:

StringFormat={}Foundation Height ({0})}">

【讨论】:

    猜你喜欢
    • 2010-10-18
    • 2018-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-11
    • 1970-01-01
    • 2014-12-27
    • 1970-01-01
    相关资源
    最近更新 更多