Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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" /> 
like image 828
Pankaj Upadhyay Avatar asked Jan 24 '11 14:01

Pankaj Upadhyay


People also ask

How do I add labels in XAML?

A XAML Label element represents a Label control. Code sample of how to use a XAML Label. The code example in Listing 1 creates a Label control in XAML and sets the name, height, width and content of a Label control. The code also sets the font format for the text.

How do I add a label in WPF?

Creating a WPF LabelThe Width and Height attributes of the Label element represent the width and the height of a Label. The Content property of the Label element sets the text of a Label. The Name attribute represents the name of the control, which is a unique identifier of a control.


1 Answers

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.

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> 
like image 143
Pavlo Glazkov Avatar answered Sep 21 '22 19:09

Pavlo Glazkov