Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 271
Zodman Avatar asked Oct 31 '11 05:10

Zodman


1 Answers

This only solution worked for me : remove FallbackValue quotes (!) and then escape the special character.

<TextBlock Text="{Binding StateCaption, FallbackValue=It couldn\'t be more weird}" />

Even the VS2017 XAML Intellisense is lost ! it displays "It" in blue, "couldn" in red, and "be more weird" in blue... but it works.

I even tested this more complexe case, and attributes following a text with spaces and without quotes are correctly interpreted :

<TextBlock Text="{Binding StateCaption, StringFormat=It couldn\'t be more weird,FallbackValue=test}" />

(Tested on VS2017, Framework 4.0)

like image 51
Elo Avatar answered Oct 14 '22 11:10

Elo