Browse Source

Merge branch 'feature/selectionmodel-changed-notifications' into feature/selectionmodel-reset-handling

pull/3498/head
Steven Kirk 6 years ago
parent
commit
a705f2b4a3
  1. 2
      src/Avalonia.Controls/SelectionModel.cs
  2. 23
      src/Avalonia.Controls/SelectionNode.cs
  3. 23
      tests/Avalonia.Controls.UnitTests/SelectionModelTests.cs

2
src/Avalonia.Controls/SelectionModel.cs

@ -326,6 +326,7 @@ namespace Avalonia.Controls
public void Dispose() public void Dispose()
{ {
ClearSelection(resetAnchor: false); ClearSelection(resetAnchor: false);
_rootNode.Cleanup();
_rootNode.Dispose(); _rootNode.Dispose();
_selectedIndicesCached = null; _selectedIndicesCached = null;
_selectedItemsCached = null; _selectedItemsCached = null;
@ -766,6 +767,7 @@ namespace Avalonia.Controls
} }
OnSelectionChanged(e); OnSelectionChanged(e);
_rootNode.Cleanup();
} }
internal class SelectedItemInfo : ISelectedItemInfo internal class SelectedItemInfo : ISelectedItemInfo

23
src/Avalonia.Controls/SelectionNode.cs

@ -375,6 +375,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) public bool Select(int index, bool select)
{ {
return Select(index, select, raiseOnSelectionChanged: true); return Select(index, select, raiseOnSelectionChanged: true);
@ -503,16 +516,6 @@ namespace Avalonia.Controls
_selectedItems?.Clear(); _selectedItems?.Clear();
SelectedCount = 0; SelectedCount = 0;
AnchorIndex = -1; 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) private bool Select(int index, bool select, bool raiseOnSelectionChanged)

23
tests/Avalonia.Controls.UnitTests/SelectionModelTests.cs

@ -1185,6 +1185,29 @@ namespace Avalonia.Controls.UnitTests
Assert.Equal(1, raised); 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] [Fact]
public void Changing_Source_Raises_SelectionChanged() public void Changing_Source_Raises_SelectionChanged()
{ {

Loading…
Cancel
Save