Browse Source

Update containers when Items changes.

Steven Kirk 9 years ago
parent
commit
2d4151ddfe

+ 12 - 0
src/Avalonia.Controls/Presenters/ItemVirtualizerSimple.cs

@@ -89,6 +89,18 @@ namespace Avalonia.Controls.Presenters
         public override void ItemsChanged(IEnumerable items, NotifyCollectionChangedEventArgs e)
         {
             base.ItemsChanged(items, e);
+
+            if (e.Action == NotifyCollectionChangedAction.Reset)
+            {
+                // We could recycle items here if this proves to be inefficient, but
+                // Reset indicates a large change and should (?) be quite rare.
+                VirtualizingPanel.Children.Clear();
+                Owner.ItemContainerGenerator.Clear();
+                FirstIndex = 0;
+                LastIndex = -1;
+                CreateRemoveContainers();
+            }
+
             ((ILogicalScrollable)Owner).InvalidateScroll();
         }
 

+ 14 - 0
tests/Avalonia.Controls.UnitTests/Presenters/ItemsPresenterTests_Virtualization.cs

@@ -184,6 +184,20 @@ namespace Avalonia.Controls.UnitTests.Presenters
                 Assert.Equal(8, target.Panel.Children.Count);
             }
 
+            [Fact]
+            public void Should_Update_Containers_When_Items_Changes()
+            {
+                var target = CreateTarget();
+
+                target.ApplyTemplate();
+                target.Measure(new Size(100, 100));
+                target.Arrange(new Rect(0, 0, 100, 100));
+
+                target.Items = new[] { "foo", "bar", "baz" };
+
+                Assert.Equal(3, target.Panel.Children.Count);
+            }
+
             public class WithContainers
             {
                 [Fact]