Explorar o código

Added failing test for #2565.

Along with a passing test I wrote looking for the solution, and some documentation about SelectionMode.
Steven Kirk %!s(int64=6) %!d(string=hai) anos
pai
achega
11a3f0c093

+ 7 - 1
src/Avalonia.Controls/ListBox.cs

@@ -68,7 +68,13 @@ namespace Avalonia.Controls
         /// <inheritdoc/>
         public new IList SelectedItems => base.SelectedItems;
 
-        /// <inheritdoc/>
+        /// <summary>
+        /// Gets or sets the selection mode.
+        /// </summary>
+        /// <remarks>
+        /// 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.
+        /// </remarks>
         public new SelectionMode SelectionMode
         {
             get { return base.SelectionMode; }

+ 4 - 0
src/Avalonia.Controls/Primitives/SelectingItemsControl.cs

@@ -222,6 +222,10 @@ namespace Avalonia.Controls.Primitives
         /// <summary>
         /// Gets or sets the selection mode.
         /// </summary>
+        /// <remarks>
+        /// 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.
+        /// </remarks>
         protected SelectionMode SelectionMode
         {
             get { return GetValue(SelectionModeProperty); }

+ 43 - 1
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<object>("bar");
 
             Assert.Equal(1, target.SelectedIndex);
+            Assert.Equal(new[] { "bar" }, target.SelectedItems);
+            Assert.Equal(new[] { 1 }, SelectedContainers(target));
+        }
+
+        [Fact]
+        public void Assigning_Multiple_SelectedItems_Should_Set_SelectedIndex()
+        {
+            // Note that we don't need SelectionMode = Multiple here. Multiple selections can always
+            // be made in code.
+            var target = new TestSelector
+            {
+                Items = new[] { "foo", "bar", "baz" },
+                Template = Template(),
+            };
+
+            target.ApplyTemplate();
+            target.Presenter.ApplyTemplate();
+            target.SelectedItems = new AvaloniaList<string>("foo", "bar", "baz");
+
+            Assert.Equal(0, target.SelectedIndex);
+            Assert.Equal(new[] { "foo", "bar", "baz" }, target.SelectedItems);
+            Assert.Equal(new[] { 0, 1, 2 }, SelectedContainers(target));
+        }
+
+        [Fact]
+        public void Selected_Items_Should_Be_Marked_When_Panel_Created_After_SelectedItems_Is_Set()
+        {
+            // Issue #2565.
+            var target = new TestSelector
+            {
+                Items = new[] { "foo", "bar", "baz" },
+                Template = Template(),
+            };
+
+            target.ApplyTemplate();
+            target.SelectedItems = new AvaloniaList<string>("foo", "bar", "baz");
+            target.Presenter.ApplyTemplate();
+
+            Assert.Equal(0, target.SelectedIndex);
+            Assert.Equal(new[] { "foo", "bar", "baz" }, target.SelectedItems);
+            Assert.Equal(new[] { 0, 1, 2 }, SelectedContainers(target));
         }
 
         [Fact]