2009-12-15 36 views
65

Tôi hiện đang có một Combobox như sau:Làm thế nào để bao gồm một ký hiệu (&) trong nội dung của một ComboBoxItem

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

Điều này đặt ra một lỗi: tài liệu tham khảo Entity hoặc chuỗi bắt đầu bằng ký hiệu '&' phải được chấm dứt bằng dấu chấm phẩy ';'.

Tôi giả sử tôi thiếu một chuỗi thoát của một số loại để cho phép tôi sử dụng &. Làm thế nào tôi có thể thiết lập nội dung của comboboxitem này để bao gồm một &? Cảm ơn

Trả lời

135

Sử dụng &amp; để mã hóa ký hiệu và.

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

Câu trả lời ngắn gọn là sử dụng &amp;-mã hóa một dấu.

cũng Entities: Handling Special Content Xem trên 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

tôi đã sửa chữa các thuật ngữ (encode vs thoát) trong câu trả lời của tôi. Cám ơn bạn đã khiến tôi chú ý tới việc này. –

+0

Liên kết của bạn có các so sánh hữu ích cho>, <, "và '. Xem bảng bắt đầu bằng" Tham chiếu thực thể \t Viết tắt của ... " – CrimsonX

+0

Thats a answer grt :) thnx :) – Apoorva

7

Ngoài ra, bạn có thể sử dụng thẻ CDATA xung quanh các nội dung của phần tử ComboBoxItem; Tôi nghĩ rằng nó tốt hơn duy trì khả năng đọc của văn bản.

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

Để tham khảo: http://www.w3schools.com/xmL/xml_cdata.asp

Các vấn đề liên quan