不能在XAML绑定的StringFormat中使用撇号?

Zod*_*man 12 c# wpf escaping string-formatting apostrophe

我正在尝试使用StringFormat在一个绑定到TextBlock的值周围插入叛逆(撇号?):

<TextBlock Text="{Binding MyValue, StringFormat='The value is &apos;{0}&apos;'}"/>
Run Code Online (Sandbox Code Playgroud)

但是,我得到一个编译错误:

MarkupExtension中的名称和值不能包含引号.MarkupExtension参数'MyValue,StringFormat ='值为'{0}''}'无效.

我注意到它确实适用于引号:

<TextBlock Text="{Binding MyValue, StringFormat='The value is &quot;{0}&quot;'}"/>
Run Code Online (Sandbox Code Playgroud)

这是StringFormat的错误吗?

K M*_*hta 17

我不确定它是不是一个bug,但我测试了这个方法,它的工作原理如下:

<TextBlock Text="{Binding MyValue, StringFormat='The value is \'{0}\''}" />
Run Code Online (Sandbox Code Playgroud)

看起来像StringFormat中的单引号必须使用\传统XML样式进行转义&apos;

  • 不幸的是,它在Silverlight中不起作用. (3认同)

Ale*_*lex 10

尝试使用\之前&apos:

<TextBlock Text="{Binding MyValue, StringFormat='The value is \&apos;{0}\&apos;'}"/>
Run Code Online (Sandbox Code Playgroud)