tags:

views:

101

answers:

2

Hi,

I have in a form two combo boxes that have the exact itemssource property. Both combo boxes need to be sorted, but in two different ways. One is sorted by the ID (numeric), the other one by Name(Alphabetic).

Is it possible to do such a thing?

Thanks

A: 

How to sort listbox and comboboxes in WPF http://www.kudzuworld.com/Blogs/Tech/20070815A.en.aspx

Since WPF does not provide a "Sort Order" property for its combo boxes, you need two different collections.

In the link I provided above, a commenter posted the following method using a ListCollectionView object to get a custom sort. This allows a single collection from your data source to be used, while adding an additional layer of collections for sorting:

// Using System.ComponentModel;
ListCollectionView view = new ListCollectionView (channel.Members);
view.SortDescriptions.Add(new SortDescription("lastName", ListSortDirection.Ascending);
view.SortDescriptions.Add(new SortDescription("firstName", ListSortDirection.Ascending); 
view.CustomSort = new IComparerImplementation; //Do this if you want a custom sort;
view.Refresh();
Robert Harvey
The correct way would be using a CollectionView over the ItemsSource. You could have multiple CollectionView, each with different sort descriptions, over a single source,
decasteljau
Why is that correct? Does it prevent a second database hit?
Robert Harvey
@Robert Harvey: Collection stays the same, read once from database or whatever. There would be two CollectionView client-side objects used as ItemsSource as an extra layer between presentation and data collection.
Stanislav Kniazev
What I did originally (to have two distinct item lists) is that I hit the databse only once, but I made a copy of my first one. It worked out great.
David Brunelle
+2  A: 

CollectionView is made just for that: http://bea.stollnitz.com/blog/?p=38

Stanislav Kniazev