public void AddGroupCategory(Category category) { var idx = DbAllCategories.IndexOf(category); var insertionPoint = DbGroupCategories.FirstIndex( item => DbAllCategories.IndexOf(item) > idx); if (insertionPoint == null) { DbGroupCategories.Add(category); } else { DbGroupCategories.Insert((int)insertionPoint, category); } }
private void AllCategoriesCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { Category cat; switch (e.Action) { case NotifyCollectionChangedAction.Add: if (e.NewItems == null) { return; } cat = ((CategoryWrapper)e.NewItems[0]).Category; DbAllCategories.Insert(e.NewStartingIndex, cat); if (_reinsert) { AddGroupCategory(cat); } _reinsert = false; break; case NotifyCollectionChangedAction.Remove: if (e.OldItems == null) { return; } var catWrap = ((CategoryWrapper)e.OldItems[0]); cat = ((CategoryWrapper)e.OldItems[0]).Category; DbAllCategories.RemoveAt(e.OldStartingIndex); if (catWrap.IsGrouping) { _reinsert = true; DbGroupCategories.Remove(cat); } break; } }