标签绑定中的%后缀

时间:2013-01-23 13:05:26

标签: wpf string-formatting

我发现了这样的事情: How to set WPF string format as percent wihout multiplying by 100?

我只想在我的号码后加上'%'号后缀。我的代码看起来像这样

<Label Content="{Binding Path=ReportGridViewModel.FillingDegree, StringFormat=P}" />

我已经尝试过这个了

<Label Content="{Binding ReportGridViewModel.Cgi, StringFormat={}{0}%}" />

在这两种情况下,我都没有看到任何变化,比如我没有任何字符串格式。

2 个答案:

答案 0 :(得分:3)

如果是Label,您需要使用ContentStringFormat属性

<Label Content="{Binding Path=ReportGridViewModel.FillingDegree}" 
       ContentStringFormat="P" />

答案 1 :(得分:3)

StringFormat的{​​{1}}属性仅在目标属性类型为Binding时应用。在String中,目标属性Label的类型为Content,因此不尊重Object

要在标签中使用此功能,请使用StringFormat。如果您使用ContentStringFormat,则可以使用TextBlock提供的StringFormat

相关问题