Skip to content

Instantly share code, notes, and snippets.

@joaoportela
Created January 24, 2013 17:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joaoportela/4625426 to your computer and use it in GitHub Desktop.
Save joaoportela/4625426 to your computer and use it in GitHub Desktop.
Snippet related to NotifyCollectionChangedAction.Move taken from the code that is used to keep two collections in sync.
case NotifyCollectionChangedAction.Move:
{
// Loosely based on http://stackoverflow.com/a/450250/86845
int newIndex = e.NewStartingIndex;
int oldIndex = e.OldStartingIndex;
int itemsCount = e.OldItems.Count;
// get a copy of the items that are to be moved
List<T> items = otherList.Skip(oldIndex).Take(itemsCount).ToList();
// remove from old indexes
otherList.RemoveRange(oldIndex, itemsCount);
// adjust for index shifts caused by removal
if (newIndex > oldIndex)
{
newIndex -= itemsCount;
}
// insert at the new index
otherList.InsertRange(newIndex, items);
}
break;
@joaoportela
Copy link
Author

joaoportela commented Aug 22, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment