Browse Source

added implementation for removing first and last elements.

Dan Walmsley 9 years ago
parent
commit
ac8185b8c3
1 changed files with 37 additions and 8 deletions
  1. 37 8
      src/Avalonia.Controls/Presenters/ItemVirtualizerSimple.cs

+ 37 - 8
src/Avalonia.Controls/Presenters/ItemVirtualizerSimple.cs

@@ -88,15 +88,44 @@ namespace Avalonia.Controls.Presenters
             switch (e.Action)
             {
                 case NotifyCollectionChangedAction.Remove:
-                    if (e.OldStartingIndex == ItemCount)
+                    if(e.OldStartingIndex >= FirstIndex && 
+                       e.OldStartingIndex + e.OldItems.Count <= NextIndex)
                     {
-                        NextIndex = ItemCount - 1;
-
-                        throw new NotImplementedException("Remove the last item from the panel.");
-                    }
-
-                    CreateRemoveContainers();
-                    RecycleContainers();
+                        if (e.OldStartingIndex == FirstIndex)
+                        {
+                            // We are removing the first in the list.
+                            VirtualizingPanel.Children.RemoveAt(e.OldStartingIndex - FirstIndex);
+                            Owner.ItemContainerGenerator.Dematerialize(e.OldStartingIndex - FirstIndex, 1);
+                            FirstIndex++; // This may not be necessary, but cant get to work without this.
+
+                            // If all items are visible we need to reduce the NextIndex too.
+                            if(NextIndex > ItemCount)
+                            {
+                                NextIndex = ItemCount;
+                            }
+
+                            CreateRemoveContainers();
+                            RecycleContainers();
+                        }
+                        else if (e.OldStartingIndex + e.OldItems.Count == NextIndex)
+                        {
+                            // We are removing the last one in the list.
+                            VirtualizingPanel.Children.RemoveAt(e.OldStartingIndex - FirstIndex);
+                            Owner.ItemContainerGenerator.Dematerialize(e.OldStartingIndex - FirstIndex, 1);
+                            NextIndex--;
+                        }
+                        else
+                        {
+                            // If all items are visible we need to reduce the NextIndex too.
+                            if (NextIndex > ItemCount)
+                            {
+                                NextIndex = ItemCount;
+                            }
+
+                            CreateRemoveContainers();
+                            RecycleContainers();
+                        }
+                    }           
                     break;
 
                 case NotifyCollectionChangedAction.Add: