WPF Xaml的StringFormat功能是否可以在Label.Content上使用? [英] Does StringFormat feature of WPF Xaml work on Label.Content?

查看:118
本文介绍了WPF Xaml的StringFormat功能是否可以在Label.Content上使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已通过DataContext将金额标签的内容属性绑定到十进制属性.我正在尝试应用stringformat,但没有效果. StringFormat 功能是否可以在Label控件上使用?请告诉我此功能在哪些控件上起作用.顺便说一句,下面是我要为其应用货币格式的标签控件的代码

I have bind my Amount Label's Content Property to a decimal property via DataContext. I am trying to apply stringformat but see no effect. Does StringFormat feature work on Label controls ?? Please tell me on which controls does this feature work. BTW following is the code for the Label Control for whom i want to apply the currency formatting

<Label Grid.Column="2" Content="{Binding Path=Amount, StringFormat={}{0:C}}" Height="23" HorizontalAlignment="Left" Margin="100,10,0,0" Name="tb" VerticalAlignment="Bottom" Width="120" />

推荐答案

StringFormat适用于字符串类型的属性(将要绑定的对象转换为采用字符串格式的字符串时). Content属性的类型为Object.

StringFormat works on properties of type string (when the object you are binding to is being converted to a string the string format is applied). The Content property is of type Object.

您可以在标签内放置一个TextBlock以获得所需的效果:

You can place a TextBlock inside your label to achieve the desired effect:

<Label Grid.Column="2" Height="23" HorizontalAlignment="Left" Margin="100,10,0,0" Name="tb" VerticalAlignment="Bottom" Width="120">
   <TextBlock Text="{Binding Path=Amount, StringFormat={}{0:C}}"/>
</Label>

这篇关于WPF Xaml的StringFormat功能是否可以在Label.Content上使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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