diff --git a/src/Avalonia.Controls/ListBox.cs b/src/Avalonia.Controls/ListBox.cs
index 3150b6be91..f26cd47bcb 100644
--- a/src/Avalonia.Controls/ListBox.cs
+++ b/src/Avalonia.Controls/ListBox.cs
@@ -68,7 +68,13 @@ namespace Avalonia.Controls
///
public new IList SelectedItems => base.SelectedItems;
- ///
+ ///
+ /// Gets or sets the selection mode.
+ ///
+ ///
+ /// Note that the selection mode only applies to selections made via user interaction.
+ /// Multiple selections can be made programatically regardless of the value of this property.
+ ///
public new SelectionMode SelectionMode
{
get { return base.SelectionMode; }
diff --git a/src/Avalonia.Controls/Primitives/SelectingItemsControl.cs b/src/Avalonia.Controls/Primitives/SelectingItemsControl.cs
index 91a9fa7e40..4f01f5467b 100644
--- a/src/Avalonia.Controls/Primitives/SelectingItemsControl.cs
+++ b/src/Avalonia.Controls/Primitives/SelectingItemsControl.cs
@@ -222,6 +222,10 @@ namespace Avalonia.Controls.Primitives
///
/// Gets or sets the selection mode.
///
+ ///
+ /// Note that the selection mode only applies to selections made via user interaction.
+ /// Multiple selections can be made programatically regardless of the value of this property.
+ ///
protected SelectionMode SelectionMode
{
get { return GetValue(SelectionModeProperty); }
@@ -338,24 +342,36 @@ namespace Avalonia.Controls.Primitives
{
base.OnContainersMaterialized(e);
- var selectedIndex = SelectedIndex;
- var selectedContainer = e.Containers
- .FirstOrDefault(x => (x.ContainerControl as ISelectable)?.IsSelected == true);
+ var resetSelectedItems = false;
- if (selectedContainer != null)
+ foreach (var container in e.Containers)
{
- SelectedIndex = selectedContainer.Index;
- }
- else if (selectedIndex >= e.StartingIndex &&
- selectedIndex < e.StartingIndex + e.Containers.Count)
- {
- var container = e.Containers[selectedIndex - e.StartingIndex];
+ if ((container.ContainerControl as ISelectable)?.IsSelected == true)
+ {
+ if (SelectedIndex == -1)
+ {
+ SelectedIndex = container.Index;
+ }
+ else
+ {
+ if (_selection.Add(container.Index))
+ {
+ resetSelectedItems = true;
+ }
+ }
- if (container.ContainerControl != null)
+ MarkContainerSelected(container.ContainerControl, true);
+ }
+ else if (_selection.Contains(container.Index))
{
MarkContainerSelected(container.ContainerControl, true);
}
}
+
+ if (resetSelectedItems)
+ {
+ ResetSelectedItems();
+ }
}
///
diff --git a/tests/Avalonia.Controls.UnitTests/Primitives/SelectingItemsControlTests_Multiple.cs b/tests/Avalonia.Controls.UnitTests/Primitives/SelectingItemsControlTests_Multiple.cs
index 9d0cc368e0..2115072e6e 100644
--- a/tests/Avalonia.Controls.UnitTests/Primitives/SelectingItemsControlTests_Multiple.cs
+++ b/tests/Avalonia.Controls.UnitTests/Primitives/SelectingItemsControlTests_Multiple.cs
@@ -53,7 +53,7 @@ namespace Avalonia.Controls.UnitTests.Primitives
}
[Fact]
- public void Assigning_SelectedItems_Should_Set_SelectedIndex()
+ public void Assigning_Single_SelectedItems_Should_Set_SelectedIndex()
{
var target = new TestSelector
{
@@ -62,9 +62,51 @@ namespace Avalonia.Controls.UnitTests.Primitives
};
target.ApplyTemplate();
+ target.Presenter.ApplyTemplate();
target.SelectedItems = new AvaloniaList