What's the XAML equivalent of .toFixed(2)? - wpf

I want to display a number in a Label, it should be formatted with 2 decimal places (always).
Example:
<Label Content="{Binding MyMoneyAmount}" />
If MyMoneyAmount = 100, then it should display as: 100.00 not 100.

<Label Content="{Binding MyMoneyAmount, StringFormat={0:F2}}" />

According to this SO Does StringFormat work on Label Content and also my own testing, StringFormat won't work on a Label as Content is of type Object as reflected in another answer on this page.
You can use this workaround (to display for example as a currency):
<Label><TextBlock Text="{Binding Source={myValue}, StringFormat={}{0:N2}}" /></Label>

Related

Why is AccessText not used when using ContentStringFormat?

When using a string value as Content of a ContentControl (like Label), using an _ inside the string allows using the following letter as access key, like this:
<Label Content="Text with _access key"/>
or even
<Label Content="{Binding Text}"/>
if Text is a string property containing an _.
However, when using ContentStringFormat which contains the _, it doesn't work any more:
<Label Content="{Binding Value}" ContentStringFormat="_Formatted value {0}"/>
I have seen in the debugger that no AccessText is used in this case.
As a workaround, I have used AccessText explicitly:
<Label>
<AccessText Text="{Binding Value, StringFormat=_Formatted value {0}"/>
</Label>
It works that way but I still want to know why it doesn't when using ContentStringFormat.
Maybe This topic will give you some insight about the AccesText.

Combine text to String.Format

I have this StringFormat StringFormat={}{0:#,0}} that represent decimal numbers and I want add text of my own after this number.
This is what I have tried (but it does not compile with the text inside the StringFormat)
Content="{Binding Path=(my:MyClass.MyStaticProperty),StringFormat={}{0:#,0}} My Text"
my:MyClass.MyStaticProperty is integer type.
As you use Label, Content cannot be formatted with StringFormat. Please view this topic.
Your solution is to use ContentStringFormat:
<Label Content="{Binding Source={x:Static my:MyClass.MyStaticProperty}}"
ContentStringFormat="{}{0:#.0} My Text"/>

Label % suffix in binding

I found something like this:
How to set WPF string format as percent wihout multiplying by 100?
I just want to have a '%' sign suffix after my number. my code looks like this
<Label Content="{Binding Path=ReportGridViewModel.FillingDegree, StringFormat=P}" />
I already tried this one too
<Label Content="{Binding ReportGridViewModel.Cgi, StringFormat={}{0}%}" />
in both cases I don't see any changes like I don't have any stringformat.
The StringFormat property of a Binding is only applied when the target property is of type String. In a Label, the target property Content, is of type Object, so StringFormat is not respected.
To get this to work in a label, use ContentStringFormat. If you were to use a TextBlock, you could use the StringFormat provided by Binding.
In the case of Label you need to use the ContentStringFormat property
<Label Content="{Binding Path=ReportGridViewModel.FillingDegree}"
ContentStringFormat="P" />

Binding to show double as int

I want to to show the duration in minutes on my control but I don't want it to show up as with decimal numbers (eg, 65 instead of 65.94503).
<TextBlock Text="{Binding Duration.TotalMinutes, StringFormat=\{0\} minutes}" />
How can I do it?
If you want an integer to display, I think that all you need to do is set the StringFormat property as follows:
<TextBlock Text="{Binding Duration.TotalMinutes, StringFormat=N0}"/>
Do that in code instead of the markup. You can unit test that easily. Make TotalMinutes a string or int instead and format it in the ViewModel.

WPF Databound Label Design Time Text

I am playing about with WPF and databinding a Label control's content:
<Label Content="{Binding Name}" />
This works a treat, however I'd like to get some text in there at design time so I can see the label. Anyone know how to do this, seems it should be simple.
Thanks
TJ
Try this:
<Label Content="{Binding Name, FallbackValue='Text'}" />

Resources