69

In my XAML file I want to display this text which contains double and single quotation marks:

You shouldn't choose "Copy if New".

None of these work:

<TextBlock Text="You shouldn't choose "Copy if New":"/>
<TextBlock Text="You shouldn't choose ""Copy if New"":"/>
<TextBlock Text="You shouldn't choose \"Copy if New\":"/>
<TextBlock Text='You shouldn't choose \"Copy if New\":'/>
<TextBlock Text='You shouldn\'t choose \"Copy if New\":'/>

I give up, can I do this in XAML?

1
  • Some books (like WPF 4 by Adam Nathan, Page 34) do describe that it can be escaped using backslash. But it doesn't work.
    – Marshal
    May 20, 2015 at 8:52

3 Answers 3

131

You should encode the special characters:

<TextBlock Text='You shouldn&apos;t choose &quot;Copy if New&quot;:'/>
  • &apos; for '
  • &quot; for "
11

If you don't want to encode special chars you could use:

<TextBlock>
  <TextBlock.Text>
     You shouldn't choose "Copy if New":
  </TextBlock.Text>
</TextBlock>
2
  • 2
    Solution is good but there are syntax errors : 1) you forgot the '/' for the "TextBlock.Text" end-tag 2) you're missing a 't' in the "TextBlock" end-tag
    – Tesseract
    Jun 11, 2020 at 9:12
  • Yes, you're right! I misspelled the code. Thanks for the correction. Sep 20 at 18:08
9

There are defined XML escapes &amp; &quot; for " and &amp; &apos; for ' -- if the XML handling in XAML doesn't interpret those properly, then start to worry.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.