02 November 2010

Swapping two elements in a bound ObservableCollection IMPROVED VERSION

Today I noticed something odd. I wanted to swap two elements in an ObservableCollection that was bound to a list box – so I could move objects up and down in the list box. I noticed that moving up always worked, and moving down led to a crash in my application with the message ‘The parameter is incorrect’ in the Application_UnhandledException method of App.Xaml. Fat lot of help that is, I can tell ya ;-)

Maybe this is a Windows Phone 7 only thing, but after a bit of experimenting I found out that you always have to change the value of the item that is last in the collection – i.e., the item with the highest index – first. This worked perfectly and I blogged about it. Later I found one exception, when I tried to swap the first and the second entry. Swapping second and first works, but the other way around gave once again ‘The parameter is incorrect’

This makes no sense at all, but I guess it has something to do with the fact that at one point in time the collection contains one object twice, and whatever is bound to it does not seem to like that very much. One of my household sayings is that were’s a will, there’s a workaround. So, second time around:

using System.Collections.Generic;
using System.Collections.ObjectModel;

using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace LocalJoost.Utilities
  public static class ObservableCollectionExtensions
    public static void Swap<T>(
       this ObservableCollection<T> collection, 
       T obj1, T obj2)
      if (!(collection.Contains(obj1) && collection.Contains(obj2))) return;
      var indexes = new List<int>
         {collection.IndexOf(obj1), collection.IndexOf(obj2)};
      if(indexes[0] == indexes[1]) return;
      var values = new List<T> {collection[indexes[0]], collection[indexes[1]]};
      collection.Insert(indexes[0], values[1]);
      collection.Insert(indexes[1], values[0]);
and now I am really done with it. It's not always high science what I blog, just things I hope that save someone else a lot of time :-)


Demogodyou's Blog said...

Yes! that's it. Man, you do save me lots of time! thank you!

Moses said...

Thanks man, It works well.

antonino spitale said...

Thanks so much! you save me a lot of time!