private void OnSourceCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { object[] oldInnerCollection = innerCollection; innerCollection = TranslateCollection(SourceCollection.Cast <object>(), filterPredicate, sortKeySelector, sortDirection).ToArray(); if (innerCollection.Contains(CurrentItem)) { SetCurrent(CurrentItem); } else { SetCurrent(CurrentItemIndex >= oldInnerCollection.Length ? innerCollection.Length : CurrentItemIndex); } IEnumerable <object> oldItems = e.OldItems.Intersect(oldInnerCollection).ToArray(); IEnumerable <object> newItems = e.NewItems.Intersect(innerCollection).ToArray(); if (oldItems.Count() > 1 || newItems.Count() > 1) { CollectionChanged.Raise(this, NotifyCollectionChangedEventArgs.Reset(oldInnerCollection, innerCollection)); return; } object oldItem = e.OldItems.FirstOrDefault(); int oldIndex = Granular.Compatibility.Array.FindIndex(oldInnerCollection, item => Granular.Compatibility.EqualityComparer <object> .Default.Equals(item, oldItem)); object newItem = e.NewItems.FirstOrDefault(); int newIndex = Granular.Compatibility.Array.FindIndex(innerCollection, item => Granular.Compatibility.EqualityComparer <object> .Default.Equals(item, newItem)); if (oldIndex == -1 && newIndex == -1 || oldItem == newItem && oldIndex == newIndex) { return; } if (Granular.Compatibility.EqualityComparer <object> .Default.Equals(oldItem, newItem)) { CollectionChanged.Raise(this, NotifyCollectionChangedEventArgs.Move(newItem, oldIndex, newIndex)); } else if (oldIndex == newIndex) { CollectionChanged.Raise(this, NotifyCollectionChangedEventArgs.Replace(oldItem, newItem, newIndex)); } else { if (oldIndex != -1) { CollectionChanged.Raise(this, NotifyCollectionChangedEventArgs.Remove(oldItem, oldIndex)); } if (newIndex != -1) { CollectionChanged.Raise(this, NotifyCollectionChangedEventArgs.Add(newItem, newIndex)); } } }
/// <summary> /// Updates the <see cref="IPagedCollectionView.TotalItemCount"/> and /// <see cref="IPagedCollectionView.ItemCount"/> properties based on the /// specified <paramref name="totalItemCount"/> and values in the view /// </summary> /// <param name="totalItemCount">The total item count</param> private void UpdateItemCounts(int totalItemCount) { int itemCount = totalItemCount; if (itemCount == -1) { // We only want to update the ItemCount when we're on a page with items int count = SourceCollection.Cast <object>().Count(); if (count > 0) { itemCount = Math.Max(ItemCount, (PageIndex * PageSize) + count); } } UpdateItemCounts(totalItemCount, itemCount); }
private void ResetInnerCollection() { object[] oldInnerCollection = innerCollection ?? new object[0]; innerCollection = TranslateCollection(SourceCollection.Cast <object>(), filterPredicate, sortKeySelector, sortDirection).ToArray(); if (innerCollection.Contains(CurrentItem)) { SetCurrent(CurrentItem); } else { SetCurrent(CurrentItemIndex >= oldInnerCollection.Length ? innerCollection.Length : CurrentItemIndex); } CollectionChanged.Raise(this, NotifyCollectionChangedEventArgs.Reset(oldInnerCollection, innerCollection)); }