4
Answers

Sort Combobox Items

J N

J N

15y
16.9k
1

Hi.

 

I have a combo box created using xaml. I bind the item source to a xml document.

 

<Items>

<Item>
<Name>David</Name>

<Age>20</Age>

</Item>

<Item>
<Name>Marcus</Name>

<Age>25</Age>

</Item>

<Item>
<Name>George</Name>

<Age>25</Age>

</Item>

</Items>

 

 

<ComboBox Name="cb1" ItemsSource="{Binding Source={StaticResource myList}, XPath=Items/Item}" DisplayMemberPath="Name"/>

 

I would like to sort the combobox items so that the items are displayd in ascending order.  

 

I manage to sort the above list by using SortDescription as below :

 

SortDescription sd = new SortDescription("Name", ListSortDirection.Ascending);

 

cb1.Items.SortDescriptions.Add(sd);

 

The combobox will display the items in the following order.

 

David

George

Marcus

 

Now, when I add a node to the xml document, I added CData to handle invalid characters. The xml document will be updated as below :

 

<Items>

<Item>
<Name>David</Name>

<Age>20</Age>

</Item>

<Item>
<Name>Marcus</Name>

<Age>25</Age>

</Item>

<Item>
<Name>George</Name>

<Age>25</Age>

</Item>

<Item>
<Name
><![CDATA[Peter&#M]]></Name>

<Age>25</Age>

</Item>

</Items>

 

When I do this, the sorting failed. The node with CData is always sorted to the top of the list.

 

Peter&#M

David

George

Marcus

 

 

Any idea what I can do to solve this problem?

 

Thanks.

Answers (4)