private void HandleItemRemoved(int index, object item) { // check if the item is in the view if (_filter != null && !_filter(item)) { return; } // compute index into view if (index < 0 || index >= _view.Count || !Equals(_view[index], item)) { index = _view.IndexOf(item); } if (index < 0) { return; } // remove item from view _view.RemoveAt(index); // keep selection on the same item if (index <= CurrentPosition) { CurrentPosition--; } // notify listeners var e = new VectorChangedEventArgs(CollectionChange.ItemRemoved, index); OnVectorChanged(e); }
private void HandleItemAdded(int index, object item) { // if the new item is filtered out of view, no work if (_filter != null && !_filter(item)) { return; } // compute insert index if (_sort.Count > 0) { // sorted: insert at sort position _sortProps.Clear(); index = _view.BinarySearch(item, this); if (index < 0) { index = ~index; } } else if (_filter != null) { // if the source is not a list (e.g. enum), then do a full refresh if (_sourceList == null) { HandleSourceChanged(); return; } // find insert index // count invisible items below the insertion point and // subtract from the number of items in the view // (counting from the bottom is more efficient for the // most common case which is appending to the source collection) var visibleBelowIndex = 0; for (int i = index; i < _sourceList.Count; i++) { if (!_filter(_sourceList[i])) { visibleBelowIndex++; } } index = _view.Count - visibleBelowIndex; } // add item to view _view.Insert(index, item); // keep selection on the same item if (index <= CurrentPosition) { CurrentPosition++; } // notify listeners var e = new VectorChangedEventArgs(CollectionChange.ItemInserted, index); OnVectorChanged(e); }