开发者

WPF: StringFormat problems with a Label

开发者 https://www.devze.com 2023-01-18 07:40 出处:网络
These versions work as expected: <DataGridTextColumn Header=\"Total Units\" Binding=\"{Binding TotalUnits, Mode=OneWay, StringFormat=N0}\"/>

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

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

TotalUnits is a Decimal.

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.

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

精彩评论

暂无评论...
验证码 换一张
取 消