How can I escape a single quote in a XAML markup extension property literal?

Drew Noakes picture Drew Noakes · Aug 25, 2009 · Viewed 9.8k times · Source

I have a value converter that formats numbers (I can't use SP1 yet unfortunately). It works fine until it gets a percentage.

Here's an example:

<TextBlock Text="{Binding Path=PercentageComplete,
                          Converter={StaticResource NumberFormatter},
                          ConverterParameter='0.00 %'}" />

Unfortunately for me when Double.ToString sees a percentage character, it multiplies the number by 100. In my case, the number is already a percentage and no conversion is needed.

In C#, this would be achieved by escaping the % character with a single quote:

(99.99).ToString("0.00 %")  // gives -> "9999 %"
(99.99).ToString("0.00 '%") // gives -> "99.99 %"

Unfortunately, I cannot use a single quote in the ConverterParameter in the above XAML markup extension. Is there a way of escaping it? I have tried doubling the single quotes and using a backslash, but both failed to compile.

Answer

Rowland Shaw picture Rowland Shaw · Aug 25, 2009

Untested, but have you tried:

<TextBlock Text="{Binding Path=PercentageComplete,
                      Converter={StaticResource NumberFormatter},
                      ConverterParameter=&quot;0.00 '%&quot;}" />