From efab1c8266d5c21633fa23d1fa29c681024e7122 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Fri, 7 Feb 2020 09:12:15 +0100 Subject: [PATCH] Fix SelectionNode.Cleanup. - Removed disposed child nodes - Don't dispose child node with descendent selection --- src/Avalonia.Controls/SelectionNode.cs | 22 ++++++++++++++----- .../SelectionModelTests.cs | 15 +++++++++++++ 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/Avalonia.Controls/SelectionNode.cs b/src/Avalonia.Controls/SelectionNode.cs index 024a7aa8e7..d93a54d458 100644 --- a/src/Avalonia.Controls/SelectionNode.cs +++ b/src/Avalonia.Controls/SelectionNode.cs @@ -342,17 +342,29 @@ namespace Avalonia.Controls } } - public void Cleanup() + public bool Cleanup() { - foreach (var child in _childrenNodes) + var result = SelectedCount == 0; + + for (var i = 0; i < _childrenNodes.Count; ++i) { - child?.Cleanup(); + var child = _childrenNodes[i]; - if (child?.SelectedCount == 0) + if (child != null) { - child.Dispose(); + if (child.Cleanup()) + { + child.Dispose(); + _childrenNodes[i] = null; + } + else + { + result = false; + } } } + + return result; } public bool Select(int index, bool select) diff --git a/tests/Avalonia.Controls.UnitTests/SelectionModelTests.cs b/tests/Avalonia.Controls.UnitTests/SelectionModelTests.cs index a024105a55..9f29dce783 100644 --- a/tests/Avalonia.Controls.UnitTests/SelectionModelTests.cs +++ b/tests/Avalonia.Controls.UnitTests/SelectionModelTests.cs @@ -930,6 +930,21 @@ namespace Avalonia.Controls.UnitTests }); } + [Fact] + public void Should_Listen_For_Changes_After_Deselect() + { + var target = new SelectionModel(); + var data = CreateNestedData(1, 2, 3); + + target.Source = data; + target.Select(1, 0); + target.Deselect(1, 0); + target.Select(1, 0); + ((AvaloniaList)data[1]).Insert(0, "foo"); + + Assert.Equal(new IndexPath(1, 1), target.SelectedIndex); + } + [Fact] public void Selecting_Item_Raises_SelectionChanged() {