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

The short answer is to use & 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>.


Use &amp; to encode the ampersand.

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

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

Tags:

Escaping

Xaml