Can't use apostrophe in StringFormat of a XAML binding?

Zodman picture Zodman · Oct 31, 2011 · Viewed 9.1k times · Source

I'm trying use StringFormat to insert apostrophies (apostrophe's?) around a value that is bound to a TextBlock:

<TextBlock Text="{Binding MyValue, StringFormat='The value is &apos;{0}&apos;'}"/>

However, I get a compile error:

Names and Values in a MarkupExtension cannot contain quotes. The MarkupExtension arguments ' MyValue, StringFormat='The value is '{0}''}' are not valid.

I do notice that it does work for quotes though:

<TextBlock Text="{Binding MyValue, StringFormat='The value is &quot;{0}&quot;'}"/>

Is this a bug with StringFormat?

Answer

K Mehta picture K Mehta · Oct 31, 2011

I'm not sure if it's a bug, but I tested this method, and it works:

<TextBlock Text="{Binding MyValue, StringFormat='The value is \'{0}\''}" />

Seems like single quotes within StringFormat have to be escaped using \ as opposed to the traditional XML style &apos;