WPF:标签的 StringFormat 问题 [英] WPF: StringFormat problems with a Label
本文介绍了WPF:标签的 StringFormat 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这些版本按预期工作:
<DataGridTextColumn Header="Total Units" Binding="{Binding TotalUnits, Mode=OneWay, StringFormat=N0}"/>
<TextBlock Text="{Binding TotalUnits, Mode=OneWay, StringFormat=N0}"/>
当我用标签尝试它时,StringFormat 被 iqnored,我得到123.000000"而不是123".
When I try it with a label, the StringFormat is iqnored and I get "123.000000" instead of "123".
<Label Content="{Binding TotalUnits, Mode=OneWay, StringFormat=N0}"/>
TotalUnits 是一个小数.
TotalUnits is a Decimal.
所以,这是怎么回事?
推荐答案
任何具有 Content
属性的东西都有一个特殊的 ContentStringFormat
属性,您必须使用它而不是指定 StringFormat在绑定中.
Anything with a Content
property has a special ContentStringFormat
property you have to use rather than specifying the StringFormat in the Binding.
像这样:
<Window.Resources xmlns:sys="clr-namespace:System;assembly=mscorlib">
<sys:Int16 x:Key="MyValue">100</sys:Int16>
</Window.Resources>
<StackPanel DataContext="{StaticResource MyValue}">
<!-- using Label -->
<Label Content="{Binding}" ContentStringFormat="{}{0:C}" />
<!-- using TextBlock-->
<TextBlock Text="{Binding, StringFormat={0:C}}" />
</StackPanel>
这篇关于WPF:标签的 StringFormat 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文