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

4

15

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.

Reverso answered 25/8, 2009 at 20:23 Comment(0)
S
13

Untested, but have you tried:

<TextBlock Text="{Binding Path=PercentageComplete,
                      Converter={StaticResource NumberFormatter},
                      ConverterParameter=&quot;0.00 '%&quot;}" />
Soloist answered 25/8, 2009 at 20:34 Comment(3)
Fantastic. Works like a charm, though I prefer this variant which also works (and I only tried after your suggestion): ConverterParameter='0.00 &quot;%'. Many thanks.Reverso
Also worth pointing out that the variant I mention doesn't cause VS to mark subsequent code in red, even though it compiles just fine.Reverso
Ironically I had something similar, but thought it'd work better the other way around, so changed it :)Soloist
I
13

The below is taken from http://msdn.microsoft.com/en-us/library/ee200269.aspx.

You can escape any character with a backslash. It does not parse the backslashes but just skips them. So:

  • 'foo\'bar' becomes foo'bar;
  • "foo\"bar" becomes foo"bar;
  • 'foo\\bar' becomes foo\bar;
  • 'foo\nbar' becomes foonbar and not a newline character.

However, markup extension parsing is quite strange. Even though Visual Studio syntax highlighting doesn't support it, any character other than \{},= is a valid value character. This means that the following constructions are legal:

  • {MyExtension Name=foo'bar} (quotes must be the first character to be considered quoting for a string; anywhere else its just copied verbatim);
  • {MyExtension Name=f oo} (spaces are legal too; this becomes f oo);
  • {MyExtension Name= foo } (spaces around the value are trimmed; this becomes foo);
  • {MyExtension Name=foo\\bar} (characters after a \ are copied verbatim, so this becomes foo\bar);
  • {MyExtension Name=foo \\ bar} (this becomes foo \ bar);
  • {MyExtension Name=foo \} bar} (and this becomes foo } bar).

Note that the \ rules apply here too: any character following a \ is copied verbatim.

Incorporate answered 10/1, 2012 at 14:10 Comment(2)
Did you tested this? I have an example here where I want to insert '\t' as a ConverterParameter. '\t' gives me 't' and '\\t' gives me '\\t'.Cleaner
Indeed, I cannot duplicate the results as prescribed above. <Grid Tag="{local:Testing 'foo \\ bar', PropertyName=foo \\ bar}" /> <-- both constructor and property values return exactly as described, as foo \\ bar.Ambrotype
C
3

You can use String.Format instead of Double.ToString

public object Convert( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture ) {
    string format = (string) parameter;

    return String.Format( format, value );
}

And in your binding expression, use the special {} escape sequence:

<TextBlock Text="{Binding PercentageComplete, Converter={StaticResource NumberFormatter}, ConverterParameter='{}{0:0.00} %'}"></TextBlock>
Cai answered 25/8, 2009 at 20:41 Comment(0)
R
2

Here is a workaround that avoids the markup extension, though it isn't a direct answer to the question.

<TextBlock>
  <TextBlock.Text>
    <Binding Path="PercentageComplete"
             Converter="{StaticResource NumberFormatter}"
             ConverterParameter="0.00 '%" />
  </TextBlock.Text>
</TextBlock>
Reverso answered 25/8, 2009 at 20:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.