public override void CommitNew ()
{
if (IsEditingItem)
throw new InvalidOperationException ("Cannot CommitNew while editing an item");
if (IsAddingNew) {
if (CurrentAddItem is IEditableObject)
((IEditableObject) CurrentAddItem).EndEdit ();
RootGroup.RemoveItem (CurrentAddItem);
if (Filter != null && !Filter (CurrentAddItem)) {
RemoveFromSourceCollection (SourceCollection.IndexOf (CurrentAddItem));
} else {
// When adding a new item, we initially put it in the root group. Once it's committed
// we need to place it in the correct subtree group.
if (Grouping) {
RootGroup.AddInSubtree (CurrentAddItem, Culture, GroupDescriptions);
}
// The item was not filtered out of the tree. Do we need to resort it?
if (SortDescriptions.Count > 0) {
// The newly added item is at the end of the array. If we're sorting, we may have to move it.
// Use a binary search to figure out where the item should be in the list and put it in there.
int actualIndex = filteredList.IndexOf (CurrentAddItem);
int sortedIndex = filteredList.BinarySearch (0, filteredList.Count - 1, CurrentAddItem, new PropertyComparer (SortDescriptions));
if (sortedIndex < 0)
sortedIndex = ~sortedIndex;
if (actualIndex != sortedIndex) {
filteredList.RemoveAt (actualIndex);
filteredList.Insert (sortedIndex, CurrentAddItem);
RaiseCollectionChanged (new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Remove, CurrentAddItem, actualIndex));
RaiseCollectionChanged (new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Add, CurrentAddItem, sortedIndex));
if (CurrentAddItem == CurrentItem)
UpdateCurrentPositionAndItem (sortedIndex, CurrentAddItem);
}
}
}
CurrentAddItem = null;
IsAddingNew = false;
UpdateCanAddNewAndRemove ();
}
}