By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,728 Members | 2,490 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,728 IT Pros & Developers. It's quick & easy.

Sort Combobox Items

P: 2
I have a combo box created using xaml. I bind the item source to a xml document.
Expand|Select|Wrap|Line Numbers
  1. <Items>
  2. <Item>
  3. <Name>David</Name>
  4. <Age>20</Age>
  5. </Item>
  6. <Item>
  7. <Name>Marcus</Name>
  8. <Age>25</Age>
  9. </Item>
  10. <Item>
  11. <Name>George</Name>
  12. <Age>25</Age>
  13. </Item>
  14. </Items>
  15.  
  16.  
  17. <ComboBox Name="cb1" ItemsSource="{Binding Source={StaticResource myList}, XPath=Items/Item}" DisplayMemberPath="Name"/>
  18.  
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 :
Expand|Select|Wrap|Line Numbers
  1. <Items>
  2. <Item>
  3. <Name>David</Name>
  4. <Age>20</Age>
  5. </Item>
  6. <Item>
  7. <Name>Marcus</Name>
  8. <Age>25</Age>
  9. </Item>
  10. <Item>
  11. <Name>George</Name>
  12. <Age>25</Age>
  13. </Item>
  14. <Item>
  15. <Name><![CDATA[Peter&#M]]></Name>
  16. <Age>25</Age>
  17. </Item>
  18. </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.
Mar 18 '08 #1
Share this Question
Share on Google+
1 Reply


P: 62
Look at creating a Item Template for the combo box. Then add 2 columns one for Name and other for age. You ban then try to sort on the column you want.

I'm not 100% on this but it is where I would start looking.
Jun 18 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.