WPF:问题的StringFormat带标签 [英] WPF: StringFormat problems with a Label

查看:441
本文介绍了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.

那么,这是怎么回事?

推荐答案

任何一个内容属性有一个特殊的 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屋!

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