WPF 使用 StringFormat 格式化标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22594958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF formatting labels with StringFormat
提问by mHelpMe
I have a WPF application. I have some labels and some datagrids which are bound to some public properties. Some of these properties are numerical values.
我有一个 WPF 应用程序。我有一些标签和一些绑定到一些公共属性的数据网格。其中一些属性是数值。
In the datagrids I have been using the line below to ensure the values only display two decimal places, which works. However when I use the same line below for my label it appears to have no effect on the display as the number shows to about 9 decimal places. I don't understand why it works for the datagrid but not the label?
在数据网格中,我一直在使用下面的行来确保值只显示两位小数,这是有效的。但是,当我使用下面的同一行作为我的标签时,它似乎对显示没有影响,因为数字显示到大约 9 位小数。我不明白为什么它适用于数据网格而不适用于标签?
StringFormat={}{0:0.##}
<Label Grid.Row="3" Grid.Column="1"
Content="{Binding Obs.Tstat, StringFormat={}{0:0.#}}"
HorizontalAlignment="Center" Foreground="{StaticResource brushLinFont}"
FontSize="13" FontWeight="Bold"/>
Updated code
更新代码
<Label Grid.Row="3" Grid.Column="1"
Content="{Binding Obs.Tstat}" ContentStringFormat="{}{0:0.#}}"
HorizontalAlignment="Center" Foreground="{StaticResource brushLinFont}"
FontSize="13" FontWeight="Bold"/>
回答by Rohit Vats
For label you need to use ContentStringFormat:
对于标签,您需要使用ContentStringFormat:
<Label Content="{Binding Obs.Tstat}" ContentStringFormat="{}{0:0.##}"/>
Reason:
原因:
Label's Contentproperty is of type objectand StringFormatis used only when binding property is of type String.
Label 的Content属性是 typeobject并且StringFormat仅在 binding 属性是 type 时使用String。
If you try your code with TextBlock's Textproperty it will work fine with StringFormatbecause Text property is of type string.
如果您使用 TextBlock 的Text属性尝试您的代码,它将正常工作,StringFormat因为 Text 属性是字符串类型。
回答by K.DW
Just a quick addition I'd like to post along these lines in case anyone else runs in to it... My application uses localization since it has multi-country support, but we also support user's system settings. We noticed ContentStringFormatdefaults to your UI culture.
只是一个快速补充,我想沿着这些路线发布,以防其他人遇到它...我的应用程序使用本地化,因为它具有多国支持,但我们也支持用户的系统设置。我们注意到ContentStringFormat默认为您的 UI 文化。
That caused an issue in one of our forms where the user's machine-specificdecimal places they had configured in windows settings were not respected when you specified the ContentStringFormat.
这在我们的表格,其中用户的一个引起的问题机器特定的小数他们当你指定的设置没有得到尊重的窗户已经配置了ContentStringFormat。
Since the ContentPresentersimply takes the string format without the converter culture you could normally specify in the binding , this means that a format of say: 0:Nwill only return two decimal places if English is my current UI culture, even though I have 5 decimals specified on my windows settings.
由于ContentPresenter只采用字符串格式,而没有您通常可以在绑定中指定的转换器文化,这意味着格式 say:0:N如果英语是我当前的 UI 文化,则只会返回两位小数,即使我指定了 5个小数我的 Windows 设置。
In our case we applied some binding overrides to work around this, but mainly just wanted to add this extra bit of info in case anyone else runs in to it ;)
在我们的例子中,我们应用了一些绑定覆盖来解决这个问题,但主要只是想添加这个额外的信息,以防其他人遇到它;)

