private static void OnItemsSourcePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { MyVirtualizingPanel2 virtualizingPanel2 = d as MyVirtualizingPanel2; if (virtualizingPanel2 == null) { return; } virtualizingPanel2.ClearItems(); INotifyCollectionChanged collection1 = e.OldValue as INotifyCollectionChanged; if (collection1 != null) { virtualizingPanel2.UnhookCollectionChanged(collection1); } INotifyCollectionChanged collection2 = e.NewValue as INotifyCollectionChanged; if (collection2 != null) { List <IVirtualizable> virtualizableList = new List <IVirtualizable>(); foreach (object obj in (IEnumerable)(collection2 as ICollection)) { if (obj is IVirtualizable) { virtualizableList.Add(obj as IVirtualizable); } else if (virtualizingPanel2.CreateVirtItemFunc != null) { virtualizableList.Add(virtualizingPanel2.CreateVirtItemFunc(obj)); } } virtualizingPanel2.AddItems((IEnumerable <IVirtualizable>)virtualizableList); virtualizingPanel2.HookUpCollectionChanged(collection2); virtualizingPanel2.SubscribeOnEdit(); } else { virtualizingPanel2.UnsubscribeOnEdit(); } }
private void collection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { MyVirtualizingPanel2 virtualizingPanel2 = this; List <IVirtualizable> itemsToInsert = new List <IVirtualizable>(); if (e.NewItems != null) { foreach (object newItem in (IEnumerable)e.NewItems) { if (newItem is IVirtualizable) { itemsToInsert.Add(newItem as IVirtualizable); } else if (this.CreateVirtItemFunc != null) { itemsToInsert.Add(this.CreateVirtItemFunc(newItem)); } } } List <IVirtualizable> virtualizableList1 = new List <IVirtualizable>(); if (e.OldItems != null) { foreach (object oldItem in (IEnumerable)e.OldItems) { if (oldItem is IVirtualizable) { virtualizableList1.Add(oldItem as IVirtualizable); } else if (e.OldStartingIndex >= 0 && e.OldStartingIndex < virtualizingPanel2.VirtualizableItems.Count) { IVirtualizable itemToBeRemoved = virtualizingPanel2.VirtualizableItems[e.OldStartingIndex]; virtualizingPanel2.RemoveItem(itemToBeRemoved); } } } if (this._isEditing) { this._addedWhileEdited.AddRange((IEnumerable <IVirtualizable>)itemsToInsert); } else if (e.Action == NotifyCollectionChangedAction.Add) { if (e.NewStartingIndex >= virtualizingPanel2.VirtualizableItems.Count) { virtualizingPanel2.AddItems((IEnumerable <IVirtualizable>)itemsToInsert); } else { virtualizingPanel2.InsertRemoveItems(e.NewStartingIndex, itemsToInsert, virtualizingPanel2.KeepScrollPositionWhenAddingItems, (IVirtualizable)null); } } else if (e.Action == NotifyCollectionChangedAction.Reset) { virtualizingPanel2.ClearItems(); List <IVirtualizable> virtualizableList2 = new List <IVirtualizable>(); foreach (object obj in (IEnumerable)virtualizingPanel2.ItemsSource) { virtualizableList2.Add(obj is IVirtualizable ? obj as IVirtualizable : this.CreateVirtItemFunc(obj)); } virtualizingPanel2.AddItems((IEnumerable <IVirtualizable>)virtualizableList2); } else { if (e.Action != NotifyCollectionChangedAction.Remove || virtualizableList1.Count <= 0) { return; } virtualizingPanel2.RemoveItem(virtualizableList1[0]); } }