From 520dc16c2a879f4806e1ce8e7580914d011035ab Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Thu, 6 Feb 2020 16:12:13 +0100 Subject: [PATCH 1/2] Added failing test. Clearing a nested selection doesn't raise `SelectionChanged`. --- .../SelectionModelTests.cs | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tests/Avalonia.Controls.UnitTests/SelectionModelTests.cs b/tests/Avalonia.Controls.UnitTests/SelectionModelTests.cs index c76dc890a9..040448d6fd 100644 --- a/tests/Avalonia.Controls.UnitTests/SelectionModelTests.cs +++ b/tests/Avalonia.Controls.UnitTests/SelectionModelTests.cs @@ -1184,6 +1184,29 @@ namespace Avalonia.Controls.UnitTests Assert.Equal(1, raised); } + [Fact] + public void Clearing_Nested_Selection_Raises_SelectionChanged() + { + var target = new SelectionModel(); + var raised = 0; + + target.Source = CreateNestedData(1, 2, 3); + target.Select(1, 1); + + target.SelectionChanged += (s, e) => + { + Assert.Equal(new[] { new IndexPath(1, 1) }, e.DeselectedIndices); + Assert.Equal(new object[] { 4 }, e.DeselectedItems); + Assert.Empty(e.SelectedIndices); + Assert.Empty(e.SelectedItems); + ++raised; + }; + + target.ClearSelection(); + + Assert.Equal(1, raised); + } + [Fact] public void Changing_Source_Raises_SelectionChanged() { From e3d11a82888ba8eef2f928a12e857c9683f0ec3e Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Thu, 6 Feb 2020 16:12:43 +0100 Subject: [PATCH 2/2] Fix clearing nested selection not raising SelectionChanged. --- src/Avalonia.Controls/SelectionModel.cs | 2 ++ src/Avalonia.Controls/SelectionNode.cs | 23 +++++++++++++---------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/Avalonia.Controls/SelectionModel.cs b/src/Avalonia.Controls/SelectionModel.cs index c8ebf59032..5d787fb1ca 100644 --- a/src/Avalonia.Controls/SelectionModel.cs +++ b/src/Avalonia.Controls/SelectionModel.cs @@ -324,6 +324,7 @@ namespace Avalonia.Controls public void Dispose() { ClearSelection(resetAnchor: false); + _rootNode.Cleanup(); _rootNode.Dispose(); _selectedIndicesCached = null; _selectedItemsCached = null; @@ -764,6 +765,7 @@ namespace Avalonia.Controls } OnSelectionChanged(e); + _rootNode.Cleanup(); } internal class SelectedItemInfo : ISelectedItemInfo diff --git a/src/Avalonia.Controls/SelectionNode.cs b/src/Avalonia.Controls/SelectionNode.cs index 04144e1ed0..81177f06ca 100644 --- a/src/Avalonia.Controls/SelectionNode.cs +++ b/src/Avalonia.Controls/SelectionNode.cs @@ -342,6 +342,19 @@ namespace Avalonia.Controls } } + public void Cleanup() + { + foreach (var child in _childrenNodes) + { + child?.Cleanup(); + + if (child?.SelectedCount == 0) + { + child.Dispose(); + } + } + } + public bool Select(int index, bool select) { return Select(index, select, raiseOnSelectionChanged: true); @@ -453,16 +466,6 @@ namespace Avalonia.Controls SelectedCount = 0; AnchorIndex = -1; - - // This will throw away all the children SelectionNodes - // causing them to be unhooked from their data source. This - // essentially cleans up the tree. - foreach (var child in _childrenNodes) - { - child?.Dispose(); - } - - _childrenNodes.Clear(); } private bool Select(int index, bool select, bool raiseOnSelectionChanged)