简体   繁体   English

WPF:String的StringFormat问题

[英]WPF: StringFormat problems with a Label

These versions work as expected: 这些版本按预期工作:

<DataGridTextColumn Header="Total Units" Binding="{Binding TotalUnits, Mode=OneWay, StringFormat=N0}"/>

<TextBlock Text="{Binding TotalUnits, Mode=OneWay, StringFormat=N0}"/>

When I try it with a label, the StringFormat is iqnored and I get "123.000000" instead of "123". 当我用标签尝试它时,StringFormat是iqnored,我得到“123.000000”而不是“123”。

<Label Content="{Binding TotalUnits, Mode=OneWay, StringFormat=N0}"/>

TotalUnits is a Decimal. TotalUnits是十进制。

So, what's going on? 发生什么了?

Anything with a Content property has a special ContentStringFormat property you have to use rather than specifying the StringFormat in the Binding. 任何具有Content属性的东西都必须使用特殊的ContentStringFormat属性,而不是在Binding中指定StringFormat。

Like this: 像这样:

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM