From a1721ba7276656cd605aacbc6dd6c1a57064235f Mon Sep 17 00:00:00 2001 From: Max Katz Date: Mon, 25 Sep 2023 16:19:59 -0700 Subject: [PATCH] Merge pull request #13019 from AvaloniaUI/fixes/itemscontrol-logical-child-removal Fix ItemsControl logical child removal --- .../Presenters/PanelContainerGenerator.cs | 3 +- .../ItemsControlTests.cs | 35 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/Avalonia.Controls/Presenters/PanelContainerGenerator.cs b/src/Avalonia.Controls/Presenters/PanelContainerGenerator.cs index 999c6db1bd..cba729d9d2 100644 --- a/src/Avalonia.Controls/Presenters/PanelContainerGenerator.cs +++ b/src/Avalonia.Controls/Presenters/PanelContainerGenerator.cs @@ -68,9 +68,10 @@ namespace Avalonia.Controls.Presenters { var c = children[index + i]; + itemsControl.RemoveLogicalChild(children[i + index]); + if (!c.IsSet(ItemIsOwnContainerProperty)) { - itemsControl.RemoveLogicalChild(children[i + index]); generator.ClearItemContainer(c); } } diff --git a/tests/Avalonia.Controls.UnitTests/ItemsControlTests.cs b/tests/Avalonia.Controls.UnitTests/ItemsControlTests.cs index 7d3c1fe4f4..f8ba4bd5fc 100644 --- a/tests/Avalonia.Controls.UnitTests/ItemsControlTests.cs +++ b/tests/Avalonia.Controls.UnitTests/ItemsControlTests.cs @@ -513,6 +513,41 @@ namespace Avalonia.Controls.UnitTests Assert.Same(before, after); } + [Fact] + public void Control_Item_Should_Be_Removed_From_LogicalChildren() + { + using var app = Start(); + var item = new Border(); + + var items = new ObservableCollection(); + var target = CreateTarget(itemsSource: items); + + items.Add(item); + items.Remove(item); + + Assert.Empty(target.LogicalChildren); + } + + [Fact] + public void Control_Item_Should_Be_Removed_From_LogicalChildren_Virtualizing() + { + using var app = Start(); + var item = new Border(); + + var items = new ObservableCollection(); + var itemsPanel = new FuncTemplate(() => new VirtualizingStackPanel()); + var target = CreateTarget( + itemsPanel: itemsPanel, + itemsSource: items); + + items.Add(item); + Layout(target); + + items.Remove(item); + + Assert.Empty(target.LogicalChildren); + } + [Fact] public void Should_Clear_Containers_When_ItemsPresenter_Changes() {