diff --git a/src/Avalonia.Controls/Primitives/SelectingItemsControl.cs b/src/Avalonia.Controls/Primitives/SelectingItemsControl.cs index 8cd714b1b8..c1db8e80ff 100644 --- a/src/Avalonia.Controls/Primitives/SelectingItemsControl.cs +++ b/src/Avalonia.Controls/Primitives/SelectingItemsControl.cs @@ -495,6 +495,8 @@ namespace Avalonia.Controls.Primitives _selection.Add(i); } + UpdateSelectedItem(0, false); + foreach (var container in ItemContainerGenerator.Containers) { MarkItemSelected(container.Index, true); @@ -507,20 +509,7 @@ namespace Avalonia.Controls.Primitives /// /// Deselects all items in the control. /// - protected void UnselectAll() - { - UpdateSelectedItems(() => - { - _selection.Clear(); - - foreach (var container in ItemContainerGenerator.Containers) - { - MarkItemSelected(container.Index, false); - } - - SelectedItems.Clear(); - }); - } + protected void UnselectAll() => UpdateSelectedItem(-1); /// /// Updates the selection for an item based on user interaction. diff --git a/tests/Avalonia.Controls.UnitTests/Primitives/SelectingItemsControlTests_Multiple.cs b/tests/Avalonia.Controls.UnitTests/Primitives/SelectingItemsControlTests_Multiple.cs index e0a8fa81f4..13dd5724c2 100644 --- a/tests/Avalonia.Controls.UnitTests/Primitives/SelectingItemsControlTests_Multiple.cs +++ b/tests/Avalonia.Controls.UnitTests/Primitives/SelectingItemsControlTests_Multiple.cs @@ -4,6 +4,7 @@ using System; using System.Collections; using System.Collections.Generic; +using System.Collections.ObjectModel; using System.Linq; using Avalonia.Collections; using Avalonia.Controls.Presenters; @@ -783,6 +784,45 @@ namespace Avalonia.Controls.UnitTests.Primitives Assert.Equal(new[] { "Foo", "Bar", "Foo", "Bar" }, target.SelectedItems); } + [Fact] + public void SelectAll_Sets_SelectedIndex_And_SelectedItem() + { + var target = new TestSelector + { + Template = Template(), + Items = new[] { "Foo", "Bar", "Baz" }, + SelectionMode = SelectionMode.Multiple, + }; + + target.ApplyTemplate(); + target.Presenter.ApplyTemplate(); + + target.SelectAll(); + + Assert.Equal(0, target.SelectedIndex); + Assert.Equal("Foo", target.SelectedItem); + } + + [Fact] + public void UnselectAll_Clears_SelectedIndex_And_SelectedItem() + { + var target = new TestSelector + { + Template = Template(), + Items = new[] { "Foo", "Bar", "Baz" }, + SelectionMode = SelectionMode.Multiple, + SelectedIndex = 0, + }; + + target.ApplyTemplate(); + target.Presenter.ApplyTemplate(); + + target.UnselectAll(); + + Assert.Equal(-1, target.SelectedIndex); + Assert.Equal(null, target.SelectedItem); + } + [Fact] public void SelectAll_Handles_Duplicate_Items() { @@ -836,9 +876,8 @@ namespace Avalonia.Controls.UnitTests.Primitives } public new void SelectAll() => base.SelectAll(); - + public new void UnselectAll() => base.UnselectAll(); public void SelectRange(int index) => UpdateSelection(index, true, true); - public void Toggle(int index) => UpdateSelection(index, true, false, true); }