简体   繁体   English

WPF Xaml的StringFormat功能是否适用于Label.Content?

[英]Does StringFormat feature of WPF Xaml work on Label.Content?

I have bind my Amount Label's Content Property to a decimal property via DataContext. 我通过DataContext将Amount Label的 Content属性绑定到十进制属性。 I am trying to apply stringformat but see no effect. 我正在尝试应用stringformat但看不到任何效果。 Does StringFormat feature work on Label controls ?? StringFormat功能是否适用于Label控件? 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 BTW以下是我想要应用货币格式的Label Control代码

<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 works on properties of type string (when the object you are binding to is being converted to a string the string format is applied). StringFormat适用于string类型的属性(当您绑定的对象正在转换为应用字符串格式的字符串时)。 The Content property is of type Object . Content属性的类型为Object

You can place a TextBlock inside your label to achieve the desired effect: 您可以在标签内放置TextBlock以获得所需的效果:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM