问题描述
我已通过 DataContext 将我的数量 标签的 内容属性绑定到十进制属性.我正在尝试应用 stringformat 但没有看到任何效果.StringFormat 功能是否适用于标签控件??请告诉我此功能在哪些控件上起作用.顺便说一句,以下是我要为其应用货币格式的标签控件的代码
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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!