/// <summary> /// Moves the given items to the front of the list. /// </summary> /// <typeparam name="TItem">The type of the items.</typeparam> /// <param name="this">This BindingList.</param> /// <param name="items">The items.</param> public static void MoveToFront <TItem>(this BindingList <TItem> @this, IEnumerable <TItem> items) { #if NET40 Contract.Requires(@this != null); Contract.Requires(items != null); #endif var raiseEvents = @this.RaiseListChangedEvents; try { @this.RaiseListChangedEvents = false; foreach (var item in items.Reverse()) { @this.Remove(item); @this.Insert(0, item); } } finally { @this.RaiseListChangedEvents = raiseEvents; @this.ResetBindings(); } }
/// <summary> /// Moves the given items relative. /// </summary> /// <typeparam name="TItem">The type of the items</typeparam> /// <param name="this">This BindingList.</param> /// <param name="items">The items.</param> /// <param name="delta">The delta.</param> public static void MoveRelative <TItem>(this BindingList <TItem> @this, IEnumerable <TItem> items, int delta) { #if NET40 Contract.Requires(@this != null); Contract.Requires(items != null); #endif if (delta == 0) { return; } var raiseEvents = @this.RaiseListChangedEvents; try { @this.RaiseListChangedEvents = false; var count = @this.Count - 1; if (delta < 0) { var start = 0; foreach (var item in items) { var index = @this.IndexOf(item); if (index < 0) { continue; } @this.RemoveAt(index); var newIndex = index + delta; @this.Insert(newIndex < 0 ? start++ : newIndex, item); } } else { var end = count; foreach (var item in items.Reverse()) { var index = @this.IndexOf(item); if (index < 0) { continue; } @this.RemoveAt(index); var newIndex = index + delta; if (newIndex > end) { #if NET40 Contract.Assume(end >= 0); #endif @this.Insert(end--, item); } else { @this.Insert(newIndex, item); } } } } finally { @this.RaiseListChangedEvents = raiseEvents; @this.ResetBindings(); } }