WPF格式化标签用的StringFormat [英] WPF formatting labels with StringFormat

查看:1289
本文介绍了WPF格式化标签用的StringFormat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF应用程序。我有一些标签和一些数据网格这必将对一些公共属性。其中一些属性是数值。



在DataGrid中我一直在使用下面的一行,确保值只显示小数点后两位,其中工程。然而,当我使用下面的相同线路用于我的标签,它似乎有在显示屏上没有任何效果的数字显示,以9位小数。 ?我不明白为什么它的工作原理为DataGrid而不是标签



 的StringFormat = {} {0:0# #} 



将;标签Grid.Row =3Grid.Column =1
含量={结合Obs.Tstat,的StringFormat = {} {0:0#}}
的Horizo​​ntalAlignment =中心前景={StaticResource的brushLinFont}
字号=13粗细=大胆/>



更新代码



 <标签Grid.Row =3Grid.Column =1
含量={结合Obs.Tstat}ContentStringFormat ={} {0 :0#}}
的Horizo​​ntalAlignment =中心前景={StaticResource的brushLinFont}
字号=13粗细=大胆/>


解决方案

有关你需要使用的 ContentStringFormat

 <标签内容={结合Obs.Tstat}ContentStringFormat ={} {0:0} ## /> 






原因



标签的内容属性类型对象字符串>的StringFormat 时才使用。



如果您使用的TextBlock的文本属性将很好地工作的StringFormat ,因为Text属性是字符串类型。


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.

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?

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

解决方案

For label you need to use ContentStringFormat:

<Label Content="{Binding Obs.Tstat}" ContentStringFormat="{}{0:0.##}"/>


Reason:

Label's Content property is of type object and StringFormat is used only when binding property is of type String.

If you try your code with TextBlock's Text property it will work fine with StringFormat because Text property is of type string.

这篇关于WPF格式化标签用的StringFormat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆