2009-12-15 15 views
65

que actualmente tienen un cuadro combinado como el siguiente:Como incluir un ampersand (&) en el contenido de un ComboBoxItem

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

Esto generará un error: Las referencias a entidades o secuencias que comienzan con un signo '&' debe terminarse con un punto y coma ';'

Supongo que me falta una secuencia de escape de algún tipo para permitirme usar un &. ¿Cómo puedo configurar el contenido de este cuadro combinado para incluir un &? Gracias

Respuesta

135

Utilice &amp; para codificar el ampersand.

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

La respuesta corta es utilizar &amp;-codificar una y comercial.

Ver también Entities: Handling Special Content en 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> .

+0

He corregido la terminología (codificar vs escape) en mi respuesta. Gracias por llamar mi atención. –

+0

Su lista contiene comparaciones útiles para>, <, "y '. Consulte la tabla que comienza con" Referencia de entidad \t Stands for ... " – CrimsonX

+0

Thats a grt answer :) thnx :) – Apoorva

7

Como alternativa, puede utilizar la etiqueta CDATA en torno a los contenidos del elemento ComboBoxItem; Creo que es mejor mantener la legibilidad del texto.

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

Como referencia: http://www.w3schools.com/xmL/xml_cdata.asp

Cuestiones relacionadas