Browse Source

Merge pull request #4876 from donandren/issues/4875

fix notification for SelectedItem changed
pull/4930/head
danwalmsley 5 years ago
committed by GitHub
parent
commit
a174f3dcd4
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      src/Avalonia.Controls/Selection/SelectionModel.cs
  2. 45
      tests/Avalonia.Controls.UnitTests/Selection/InternalSelectionModelTests.cs

3
src/Avalonia.Controls/Selection/SelectionModel.cs

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

45
tests/Avalonia.Controls.UnitTests/Selection/InternalSelectionModelTests.cs

@ -205,6 +205,49 @@ namespace Avalonia.Controls.UnitTests.Selection
Assert.Equal(2, target.SelectedIndex);
}
[Fact]
public void Raises_Selection_Changed_On_Items_Reset()
{
var items = new ResettingCollection(new[] { "foo", "bar", "baz" });
var target = CreateTarget(source: items);
target.SelectedIndex = 1;
var changed = new List<string>();
target.PropertyChanged += (s, e) => changed.Add(e.PropertyName);
var oldSelectedIndex = target.SelectedIndex;
var oldSelectedItem = target.SelectedItem;
items.Reset(new string[0]);
Assert.NotEqual(oldSelectedIndex, target.SelectedIndex);
Assert.NotEqual(oldSelectedItem, target.SelectedItem);
Assert.Equal(-1, target.SelectedIndex);
Assert.Equal(null, target.SelectedItem);
Assert.Contains(nameof(target.SelectedIndex), changed);
Assert.Contains(nameof(target.SelectedItem), changed);
}
[Fact]
public void Preserves_SelectedItem_On_Items_Reset()
{
var items = new ResettingCollection(new[] { "foo", "bar", "baz" });
var target = CreateTarget(source: items);
target.SelectedItem = "foo";
Assert.Equal(0, target.SelectedIndex);
items.Reset(new string[] { "baz", "foo", "bar" });
Assert.Equal("foo", target.SelectedItem);
Assert.Equal(1, target.SelectedIndex);
}
[Fact]
public void Preserves_Selection_On_Source_Changed()
{
@ -222,7 +265,7 @@ namespace Avalonia.Controls.UnitTests.Selection
bool nullSource = false)
{
source ??= !nullSource ? new[] { "foo", "bar", "baz" } : null;
var result = new InternalSelectionModel
{
SingleSelect = singleSelect,

Loading…
Cancel
Save