Ever Need a Sortable ObservableCollection?


Recently I had a ListBox bound to an ObservableCollection.  The powers that be wanted me to sort the items in the collection in alphabetical order.  Much to my surprise I found that this was not something built into a ListBox or into the ObservableCollection.  So, I set out to figure out how to do this in WPF.  I found a very cool posting on the Internet where this programmer (I’ll have to find his name and post it to give him credit) that showed me how to make a new class called SortableObservableCollection that extends the ObservableCollection but also adds functionality to sort it.  So, I built and it worked great.  So, I am sharing my code with you.  You can download my sample application here:

http://tw0.us/5Fz

You can either just add the reference assembly (found in the Debug/Bin folder of the zip) or you can just create a new class called SortableObservableCollection and copy the code from my class with the same name.

Enjoy!

Victor

Advertisements
    • Kjartan
    • December 4th, 2009

    I would find it useful to do a follow-up to your ObservableCollection tutorial that extends it in common ways:

    First, by including the ability to Add, Remove, and Edit customers. For adding,
    there are First Name, Last Name, and Image URL textboxes.

    Second, how does the code need to change if the Add, Remove, and Edit is invoked
    from a different page in the project?

    As a Silverlight and WPF newcomer, I’m finding these sorts of common use cases
    are glossed over, and it’s hard to find step-by-step examples.

    Thanks!

    • tburkins
    • January 15th, 2010

    Hi Victor,

    Does the PagedCollectionView exist in WPF? I am an SL dev and was just curious if there was an advantage to creating a SortableObservableCollection over using PagedCollectionView and adding a SortDescription. I ran into the same requirement in an SL app that I am working on. I started by creating a SortableObservableCollection and then switched to the PagedCollectionView because it seemed like the way it should be done, although I don’t know. I’m somewhat new to Silverlight and am just trying to figure out why one approach might be better than another approach.

    Thanks

  1. I could not find a PagedCollectionView in WPF or Silverlight. I just tried to create one in Code-Behind and could not resolve that name. Can you paste sample code showing how you use it? And, what is the namespace that this control resoves too? Victor

  1. December 3rd, 2009

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: