Example #1
0
        private void OnItemContainerGeneratorChanged(object sender, ItemsChangedEventArgs e)
        {
            if (this.itemsPresenter == null || this.itemsPresenter.Child is VirtualizingPanel)
            {
                return;
            }

            Panel panel = this.itemsPresenter.Child;

            switch (e.Action)
            {
                case NotifyCollectionChangedAction.Reset:
                    // the list has gone away, so clear the children of the panel
                    if (panel.InternalChildren.Count > 0)
                    {
                        this.RemoveItemsFromPresenter(new GeneratorPosition(0, 0), panel.InternalChildren.Count);
                    }

                    break;
                case NotifyCollectionChangedAction.Add:
                    this.AddItemsToPresenter(e.Position, e.ItemCount);
                    break;
                case NotifyCollectionChangedAction.Remove:
                    this.RemoveItemsFromPresenter(e.Position, e.ItemCount);
                    break;
                case NotifyCollectionChangedAction.Replace:
                    this.RemoveItemsFromPresenter(e.Position, e.ItemCount);
                    this.AddItemsToPresenter(e.Position, e.ItemCount);
                    break;
            }
        }
        internal void OnOwnerItemsItemsChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            int itemCount;
            int itemUICount;
            GeneratorPosition oldPosition = new GeneratorPosition(-1, 0);
            GeneratorPosition position;

            switch (e.Action)
            {
                case NotifyCollectionChangedAction.Add:
                    if ((e.NewStartingIndex + 1) != this.Owner.Items.Count)
                    {
                        this.MoveExistingItems(e.NewStartingIndex, 1);
                    }

                    itemCount = 1;
                    itemUICount = 0;
                    position = this.GeneratorPositionFromIndex(e.NewStartingIndex);
                    break;

                case NotifyCollectionChangedAction.Remove:
                    itemCount = 1;
                    itemUICount = this.RealizedElements.Contains(e.OldStartingIndex) ? 1 : 0;
                    position = this.GeneratorPositionFromIndex(e.OldStartingIndex);
                    
                    if (itemUICount == 1)
                    {
                        this.Remove(position, 1);
                    }

                    this.MoveExistingItems(e.OldStartingIndex, -1);
                    break;

                case NotifyCollectionChangedAction.Replace:
                    if (!this.RealizedElements.Contains(e.NewStartingIndex))
                    {
                        return;
                    }

                    itemCount = 1;
                    itemUICount = 1;
                    position = this.GeneratorPositionFromIndex(e.NewStartingIndex);
                    
                    this.Remove(position, 1);

                    bool fresh;
                    var newPos = this.GeneratorPositionFromIndex(e.NewStartingIndex);
                    using (this.StartAt(newPos, GeneratorDirection.Forward, true))
                    {
                        this.PrepareItemContainer(this.GenerateNext(out fresh));
                    }

                    break;

                case NotifyCollectionChangedAction.Reset:
                    itemCount = e.OldItems == null ? 0 : e.OldItems.Count;
                    itemUICount = this.RealizedElements.Count;
                    position = new GeneratorPosition(-1, 0);
                    this.RemoveAll();
                    break;

                default:
                    Console.WriteLine("*** Critical error in ItemContainerGenerator.OnOwnerItemsItemsChanged. NotifyCollectionChangedAction.{0} is not supported", e.Action);
                    return;
            }

            ItemsChangedEventArgs args = new ItemsChangedEventArgs
            {
                Action = e.Action,
                ItemCount = itemCount,
                ItemUICount = itemUICount,
                OldPosition = oldPosition,
                Position = position
            };

            var h = this.ItemsChanged;

            if (h != null)
            {
                h(this, args);
            }
        }