Răsfoiți Sursa

Merge pull request #4947 from donandren/issues/4945

fix notification for selecteditem in listbox
Dan Walmsley 5 ani în urmă
părinte
comite
f7acecb2da

+ 1 - 0
src/Avalonia.Controls/Selection/SelectionModel.cs

@@ -443,6 +443,7 @@ namespace Avalonia.Controls.Selection
             }
 
             if ((e.Action == NotifyCollectionChangedAction.Remove && e.OldStartingIndex <= oldSelectedIndex) ||
+                (e.Action == NotifyCollectionChangedAction.Replace && e.OldStartingIndex == oldSelectedIndex) ||
                 e.Action == NotifyCollectionChangedAction.Reset)
             {
                 RaisePropertyChanged(nameof(SelectedItem));

+ 7 - 0
tests/Avalonia.Controls.UnitTests/Selection/SelectionModelTests_Multiple.cs

@@ -1216,6 +1216,7 @@ namespace Avalonia.Controls.UnitTests.Selection
                 var data = (AvaloniaList<string>)target.Source!;
                 var selectionChangedRaised = 0;
                 var selectedIndexRaised = 0;
+                var selectedItemRaised = 0;
                 var indexesChangedRaised = 0;
 
                 target.Source = data;
@@ -1227,6 +1228,11 @@ namespace Avalonia.Controls.UnitTests.Selection
                     {
                         ++selectedIndexRaised;
                     }
+
+                    if (e.PropertyName == nameof(target.SelectedItem))
+                    {
+                        ++selectedItemRaised;
+                    }
                 };
 
                 target.IndexesChanged += (s, e) => ++indexesChangedRaised;
@@ -1249,6 +1255,7 @@ namespace Avalonia.Controls.UnitTests.Selection
                 Assert.Equal(2, target.AnchorIndex);
                 Assert.Equal(1, selectionChangedRaised);
                 Assert.Equal(1, selectedIndexRaised);
+                Assert.Equal(1, selectedItemRaised);
                 Assert.Equal(0, indexesChangedRaised);
             }
 

+ 7 - 0
tests/Avalonia.Controls.UnitTests/Selection/SelectionModelTests_Single.cs

@@ -1040,6 +1040,7 @@ namespace Avalonia.Controls.UnitTests.Selection
                 var data = (AvaloniaList<string>)target.Source!;
                 var selectionChangedRaised = 0;
                 var selectedIndexRaised = 0;
+                var selectedItemRaised = 0;
 
                 target.Source = data;
                 target.Select(1);
@@ -1050,6 +1051,11 @@ namespace Avalonia.Controls.UnitTests.Selection
                     {
                         ++selectedIndexRaised;
                     }
+
+                    if (e.PropertyName == nameof(target.SelectedItem))
+                    {
+                        ++selectedItemRaised;
+                    }
                 };
 
                 target.SelectionChanged += (s, e) =>
@@ -1070,6 +1076,7 @@ namespace Avalonia.Controls.UnitTests.Selection
                 Assert.Equal(-1, target.AnchorIndex);
                 Assert.Equal(1, selectionChangedRaised);
                 Assert.Equal(1, selectedIndexRaised);
+                Assert.Equal(1, selectedItemRaised);
             }
 
             [Fact]