diff --git a/src/Avalonia.Controls/Primitives/SelectingItemsControl.cs b/src/Avalonia.Controls/Primitives/SelectingItemsControl.cs index a2f4793958..ab07b3ffa9 100644 --- a/src/Avalonia.Controls/Primitives/SelectingItemsControl.cs +++ b/src/Avalonia.Controls/Primitives/SelectingItemsControl.cs @@ -999,26 +999,6 @@ namespace Avalonia.Controls.Primitives SelectedItemProperty, oldItem, item); - - UpdateSelectedItems(() => - { - if (clear) - { - SelectedItems.Clear(); - - if (index != -1) - { - SelectedItems.Add(item); - } - } - else - { - if (added != -1) - { - SelectedItems.Add(added); - } - } - }); } if (removed != null && index != -1) @@ -1028,6 +1008,8 @@ namespace Avalonia.Controls.Primitives if (added != -1 || removed?.Count > 0) { + ResetSelectedItems(); + var e = new SelectionChangedEventArgs( SelectionChangedEvent, added != -1 ? new[] { ElementAt(Items, added) } : Array.Empty(), diff --git a/tests/Avalonia.Controls.UnitTests/Primitives/SelectingItemsControlTests_Multiple.cs b/tests/Avalonia.Controls.UnitTests/Primitives/SelectingItemsControlTests_Multiple.cs index d8de98f4f2..4167a8e490 100644 --- a/tests/Avalonia.Controls.UnitTests/Primitives/SelectingItemsControlTests_Multiple.cs +++ b/tests/Avalonia.Controls.UnitTests/Primitives/SelectingItemsControlTests_Multiple.cs @@ -957,6 +957,30 @@ namespace Avalonia.Controls.UnitTests.Primitives Assert.Equal(new[] { "Foo", "Qux", "Baz" }, target.SelectedItems); } + [Fact] + public void Left_Click_On_SelectedItem_Should_Clear_Existing_Selection() + { + var target = new ListBox + { + Template = Template(), + Items = new[] { "Foo", "Bar", "Baz" }, + ItemTemplate = new FuncDataTemplate(x => new TextBlock { Width = 20, Height = 10 }), + SelectionMode = SelectionMode.Multiple, + }; + + target.ApplyTemplate(); + target.Presenter.ApplyTemplate(); + target.SelectAll(); + + Assert.Equal(3, target.SelectedItems.Count); + + _helper.Down((Interactive)target.Presenter.Panel.Children[0]); + + Assert.Equal(1, target.SelectedItems.Count); + Assert.Equal(new[] { "Foo", }, target.SelectedItems); + Assert.Equal(new[] { 0 }, SelectedContainers(target)); + } + private IEnumerable SelectedContainers(SelectingItemsControl target) { return target.Presenter.Panel.Children