CrimsonX
CrimsonX

Reputation: 9248

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

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 &?

Upvotes: 97

Views: 46817

Answers (3)

Sinan &#220;n&#252;r
Sinan &#220;n&#252;r

Reputation: 118148

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>.

Upvotes: 24

chaosTechnician
chaosTechnician

Reputation: 1700

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

Upvotes: 8

Andy West
Andy West

Reputation: 12509

Use &amp; to encode the ampersand.

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

Upvotes: 193

Related Questions