I have a field which contains a percantage value as a decimal.
A text block is bound to this field and I want to display it with a % symbol.
For example: If myfield = 5.0 then text should be displayed as "5.00 %"
This is my binding:
Text="{Binding Path=myfield, StringFormat=0.00## \%}"
which currently displays "500.00 %"
The '\' does not appear to be escaping the '%'.
How can I escape the '%' in the XAML StringFormat?
Looks like we need to double the slashes, I think the \ itself need to be escaped to help XAML compiler understand that it is a single \ before it being combined with % to make a perfect escape for % which will be understood as literal string % by the StringFormat (not by XAML compiler):
Text="{Binding Path=myfield, StringFormat=0.00## \\%}"
So technically we have 2 interpreters here, one by XAML compiler and one by the StringFormat engine. Note that the \ is also used to escape some special characters in XAML, such as the { and }.
By specifying the parameter index explicitly, you can do this:
Text="{Binding Path=myfield, StringFormat=\{0:0.00## \\%\}}"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With