2011-10-31 30 views
8

我試圖使用的StringFormat插入apostrophies(單引號的?)圍繞綁定到一個TextBlock值:在XAML綁定的StringFormat中不能使用撇號?

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

不過,我得到一個編譯錯誤:

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

我做的通知它確實適用於報價:

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

這是一個StringFormat的錯誤嗎?

回答

13

我不知道這是否是一個錯誤,但我測試過這個方法,它的工作原理:

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

好像中的StringFormat單引號都使用\進行轉義,而不是傳統的XML風格&apos;

+0

是的,這對我來說太的作品。 *呼*! – Zodman

+3

不幸的是,它不適用於Silverlight。 – Rajiv

6

嘗試使用\&apos

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