/// <summary> /// If a new item has been added to the list, and <paramref name="itemIndex"/> is the position of that item, /// remove it from the list and cancel the add operation. /// </summary> /// <param name="itemIndex">Index of item to be removed as a result of the cancellation of a previous addition.</param> void ICancelAddNew.CancelNew(int itemIndex) { if (_addNewIndex >= 0 && itemIndex == _addNewIndex) { TElement item = _viewData.List[_addNewIndex]; _listener.UnregisterEntityEvents(item); int oldIndex = _addNewIndex; // Reset the addNewIndex here so that the IObjectView.CollectionChanged method // will not attempt to examine the item being removed. // See IObjectView.CollectionChanged method for details. _addNewIndex = -1; try { _suspendEvent = true; _viewData.Remove(item, true); } finally { _suspendEvent = false; } OnListChanged(ListChangedType.ItemDeleted, oldIndex, -1); } }
public ListChangedEventArgs OnCollectionChanged(object sender, CollectionChangeEventArgs e, ObjectViewListener listener) { ListChangedEventArgs changeArgs = null; // Since event is coming from cache and it might be shared amoung different queries // we have to check to see if correct event is being handled. if (e.Element.GetType().IsAssignableFrom(typeof(TElement)) && _bindingList.Contains((TElement)(e.Element))) { TElement item = (TElement)e.Element; int itemIndex = _bindingList.IndexOf(item); if (itemIndex >= 0) // Ignore entities that we don't know about. { // Only process "remove" events. Debug.Assert(e.Action != CollectionChangeAction.Refresh, "Cache should never fire with refresh, it does not have clear"); if (e.Action == CollectionChangeAction.Remove) { _bindingList.Remove(item); listener.UnregisterEntityEvents(item); changeArgs = new ListChangedEventArgs(ListChangedType.ItemDeleted, itemIndex /* newIndex*/, -1 /* oldIndex*/); } } } return(changeArgs); }
public ListChangedEventArgs OnCollectionChanged(object sender, CollectionChangeEventArgs e, ObjectViewListener listener) { ListChangedEventArgs changeArgs = null; switch (e.Action) { case CollectionChangeAction.Remove: // An Entity is being removed from entity collection, remove it from list. if (e.Element is TViewElement) { TViewElement removedItem = (TViewElement)e.Element; int oldIndex = _bindingList.IndexOf(removedItem); if (oldIndex != -1) { _bindingList.Remove(removedItem); // Unhook from events of removed entity. listener.UnregisterEntityEvents(removedItem); changeArgs = new ListChangedEventArgs(ListChangedType.ItemDeleted, oldIndex /* newIndex*/, -1 /* oldIndex*/); } } break; case CollectionChangeAction.Add: // Add the entity to our list. if (e.Element is TViewElement) { // Do not process Add events that fire as a result of committing an item to the entity collection. if (!_itemCommitPending) { TViewElement addedItem = (TViewElement)e.Element; _bindingList.Add(addedItem); // Register to its events. listener.RegisterEntityEvents(addedItem); changeArgs = new ListChangedEventArgs(ListChangedType.ItemAdded, _bindingList.Count - 1 /* newIndex*/, -1 /* oldIndex*/); } } break; case CollectionChangeAction.Refresh: foreach (TViewElement entity in _bindingList) { listener.UnregisterEntityEvents(entity); } _bindingList.Clear(); foreach (TViewElement entity in _entityCollection.GetInternalEnumerable()) { _bindingList.Add(entity); listener.RegisterEntityEvents(entity); } changeArgs = new ListChangedEventArgs(ListChangedType.Reset, -1 /*newIndex*/, -1 /*oldIndex*/); break; } return(changeArgs); }