Пример #1
0
        private void collection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            MyVirtualizingPanel   virtualizingPanel = 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);
                    }
                }
            }
            List <IVirtualizable> virtualizableList = new List <IVirtualizable>();

            if (e.OldItems != null)
            {
                foreach (object oldItem in (IEnumerable)e.OldItems)
                {
                    if (oldItem is IVirtualizable)
                    {
                        virtualizableList.Add(oldItem as IVirtualizable);
                    }
                }
            }
            if (e.Action == NotifyCollectionChangedAction.Add)
            {
                if (e.NewStartingIndex >= virtualizingPanel.VirtualizableItems.Count)
                {
                    virtualizingPanel.AddItems((IEnumerable <IVirtualizable>)itemsToInsert);
                }
                else
                {
                    virtualizingPanel.InsertRemoveItems(e.NewStartingIndex, itemsToInsert, virtualizingPanel.KeepScrollPositionWhenAddingItems, (IVirtualizable)null);
                }
            }
            else if (e.Action == NotifyCollectionChangedAction.Reset)
            {
                virtualizingPanel.ClearItems();
                virtualizingPanel.AddItems((IEnumerable <IVirtualizable>)virtualizingPanel.ItemsSource);
            }
            else
            {
                if (e.Action != NotifyCollectionChangedAction.Remove || virtualizableList.Count <= 0)
                {
                    return;
                }
                virtualizingPanel.RemoveItem(virtualizableList[0]);
            }
        }
Пример #2
0
        private void collection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            MyVirtualizingPanel   virtualizingPanel = this;
            List <IVirtualizable> itemsToInsert     = new List <IVirtualizable>();

            if (e.NewItems != null)
            {
                IEnumerator enumerator = e.NewItems.GetEnumerator();
                try
                {
                    while (enumerator.MoveNext())
                    {
                        object current = enumerator.Current;
                        if (current is IVirtualizable)
                        {
                            itemsToInsert.Add(current as IVirtualizable);
                        }
                    }
                }
                finally
                {
                    IDisposable disposable = enumerator as IDisposable;
                    if (disposable != null)
                    {
                        disposable.Dispose();
                    }
                }
            }
            List <IVirtualizable> virtualizableList = new List <IVirtualizable>();

            if (e.OldItems != null)
            {
                IEnumerator enumerator = e.OldItems.GetEnumerator();
                try
                {
                    while (enumerator.MoveNext())
                    {
                        object current = enumerator.Current;
                        if (current is IVirtualizable)
                        {
                            virtualizableList.Add(current as IVirtualizable);
                        }
                    }
                }
                finally
                {
                    IDisposable disposable = enumerator as IDisposable;
                    if (disposable != null)
                    {
                        disposable.Dispose();
                    }
                }
            }
            if (e.Action == NotifyCollectionChangedAction.Add)
            {
                if (e.NewStartingIndex >= virtualizingPanel.VirtualizableItems.Count)
                {
                    virtualizingPanel.AddItems((IEnumerable <IVirtualizable>)itemsToInsert);
                }
                else
                {
                    virtualizingPanel.InsertRemoveItems(e.NewStartingIndex, itemsToInsert, virtualizingPanel.KeepScrollPositionWhenAddingItems, null);
                }
            }
            else if (e.Action == NotifyCollectionChangedAction.Reset)
            {
                virtualizingPanel.ClearItems();
                virtualizingPanel.AddItems((IEnumerable <IVirtualizable>)virtualizingPanel.ItemsSource);
            }
            else
            {
                if (e.Action != NotifyCollectionChangedAction.Remove || virtualizableList.Count <= 0)
                {
                    return;
                }
                virtualizingPanel.RemoveItem(virtualizableList[0]);
            }
        }