Home > DeveloperSection > Forums > Sorting a ListBox in WPF
F Scott Fitzgerald
F Scott Fitzgerald

Total Post:29

Points:203
Posted on    July-18-2013 2:01 AM

 WPF WPF 
Ratings:


 1 Reply(s)
 942  View(s)
Rate this:
Hi mindstick!

Let me start off by saying that I am completely new with WPF (this is my first project and I have been working in it for less than a week). With that being said, please be easy on me!

I have three list ListBoxes that are being bound to ObservableCollections from a LINQ queries. In the beginning, everything is fine, all three are populated correctly. My client needs to drag and drop selections from one ListBox to another. I also have this working, but when I do the drag and drop, the new selection is placed at the bottom of the ListBox instead of being sorted alphabetically with the existing items.

How can I sort the ListBox at runtime through code behind after the drag and drop operation is complete.

Thanks!



shreesh chandra shukla
shreesh chandra shukla

Total Post:105

Points:735
Posted on    July-18-2013 2:40 AM

Solution!

It is not entirely clear how you handle drag and drop in your code. You say that your ListBoxes are all data-bound - which implies that you actually move items from one backing collection to another on drag and drop. If so, ListBox just displays the items in order they are present in the collection. You should either sort them there, or, if sorting is a view-only behavior in your case (i.e. items are actually unordered in data model, by design), you should use CollectionView to wrap your collections, set it up to do the sorting, and bind the ListBoxes to that.


Don't want to miss updates? Please click the below button!

Follow MindStick