How to include an ampersand (&) in the content of a ComboBoxItem

XamlEscaping

Xaml Problem Overview


I currently have a Combobox like the following:

//XAML
<ComboBox>
<ComboBoxItem> Awake & Alive</ComboBoxItem>
</ComboBox>

This raises an error: Entity references or sequences beginning with an ampersand '&' must be terminated with a semicolon ';'.

I assume I am missing an escape sequence of some sort to allow me to use a &. How can I set the content of this comboboxitem to include a &?

Xaml Solutions


Solution 1 - Xaml

Use &amp; to encode the ampersand.

//XAML
<ComboBox>
<ComboBoxItem> Awake &amp; Alive</ComboBoxItem>
</ComboBox>

Solution 2 - Xaml

The short answer is to use &amp; to encode an ampersand.

See also Entities: Handling Special Content on XML.com:

> At the lowest levels an XML parser is just a program that reads through an XML document a character at a time and analyzes it in one way or another, then behaves accordingly. It knows that it's got to process some content differently than other content. What distinguishes these special cases is the presence of such characters as "&" and "<". They act as flags to the parser; they delimit the document's actual content, alerting the parser to the fact that it must do something at this point other than simply pass the adjacent content to some downstream application. > > ... So one way to get around your immediate problem is to replace the ampersand in your content with the appropriate entity reference: <company>Harris &amp; George</company>.

Solution 3 - Xaml

Alternatively, you can use the CDATA tag around the contents of the ComboBoxItem element; I think it better maintains the text's readability.

//XAML
<ComboBox>
<ComboBoxItem><![CDATA[Awake & Alive]]></ComboBoxItem>
</ComboBox>

For reference: http://www.w3schools.com/xmL/xml_cdata.asp

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCrimsonXView Question on Stackoverflow
Solution 1 - XamlAndy WestView Answer on Stackoverflow
Solution 2 - XamlSinan ÜnürView Answer on Stackoverflow
Solution 3 - XamlchaosTechnicianView Answer on Stackoverflow