【问题标题】:WPF: StringFormat problems with a LabelWPF:标签的StringFormat问题
【发布时间】:2011-04-19 02:41:29
【问题描述】:

这些版本按预期工作:

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

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

当我尝试使用标签时,StringFormat 被 iqnored,我得到“123.000000”而不是“123”。

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

TotalUnits 是一个小数。

那么,发生了什么事?

【问题讨论】:

    标签: wpf binding


    【解决方案1】:

    任何带有Content 属性的东西都有一个特殊的ContentStringFormat 属性,您必须使用它而不是在绑定中指定StringFormat。

    像这样:

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

    【讨论】:

      猜你喜欢
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-05
      • 1970-01-01
      • 2015-12-29
      • 2018-11-22
      • 2012-04-02
      • 2011-08-17
      相关资源
      最近更新 更多