// remove item from view 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 || !object.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 <= _index) { _index--; } // notify listeners var e = new VectorChangedEventArgs(CollectionChange.ItemRemoved, index, item); OnVectorChanged(e); }
private void _notifyVectorChanged(VectorChangedEventArgs args) { if (VectorChanged != null) { VectorChanged(this, args); } }
//------------------------------------------------------------------------------------ #region ** implementation // add item to view 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 <= _index) { _index++; } // notify listeners var e = new VectorChangedEventArgs(CollectionChange.ItemInserted, index, item); OnVectorChanged(e); }
public void When_Converting_Args() { var add = new VectorChangedEventArgs(CollectionChange.ItemInserted, 12); var addC = add.ToNotifyCollectionChangedEventArgs(); Assert.AreEqual((int)add.Index, addC.NewStartingIndex); Assert.AreEqual(1, addC.NewItems.Count); var remove = new VectorChangedEventArgs(CollectionChange.ItemRemoved, 15); var removeC = remove.ToNotifyCollectionChangedEventArgs(); Assert.AreEqual((int)remove.Index, removeC.OldStartingIndex); Assert.AreEqual(1, removeC.OldItems.Count); var replace = new VectorChangedEventArgs(CollectionChange.ItemChanged, 3); var replaceC = replace.ToNotifyCollectionChangedEventArgs(); Assert.AreEqual((int)replace.Index, replaceC.NewStartingIndex); Assert.AreEqual(1, replaceC.NewItems.Count); Assert.AreEqual(1, replaceC.OldItems.Count); var reset = new VectorChangedEventArgs(CollectionChange.Reset, 0); var resetC = reset.ToNotifyCollectionChangedEventArgs(); }
private void _notifyReplace(int index) { var args = new VectorChangedEventArgs(CollectionChange.ItemChanged, (uint)index); _notifyVectorChanged(args); }
private void _notifyReset() { var args = new VectorChangedEventArgs(CollectionChange.Reset, 0); _notifyVectorChanged(args); }