Browse Source

Merge pull request #9699 from AvaloniaUI/fixes/7974-selectionmodel-eventargs-indexer

Fix SelectionModelSelectionChangedEventArgs.SelectedItems indexer.
pull/9702/head
Max Katz 4 years ago
committed by GitHub
parent
commit
cd0020eb75
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      src/Avalonia.Controls/Selection/SelectedItems.cs
  2. 19
      tests/Avalonia.Controls.UnitTests/Selection/SelectionModelTests_Single.cs

4
src/Avalonia.Controls/Selection/SelectedItems.cs

@ -35,9 +35,9 @@ namespace Avalonia.Controls.Selection
{ {
return _owner.SelectedItem; return _owner.SelectedItem;
} }
else if (Items is object) else if (Items is not null && Ranges is not null)
{ {
return Items[index]; return Items[IndexRange.GetAt(Ranges, index)];
} }
else else
{ {

19
tests/Avalonia.Controls.UnitTests/Selection/SelectionModelTests_Single.cs

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Specialized; using System.Collections.Specialized;
using System.Linq;
using Avalonia.Collections; using Avalonia.Collections;
using Avalonia.Controls.Selection; using Avalonia.Controls.Selection;
using Avalonia.Controls.Utils; using Avalonia.Controls.Utils;
@ -1144,6 +1145,24 @@ namespace Avalonia.Controls.UnitTests.Selection
Assert.Equal(new[] { "foo" }, target.SelectedItems); Assert.Equal(new[] { "foo" }, target.SelectedItems);
Assert.Equal(0, target.AnchorIndex); Assert.Equal(0, target.AnchorIndex);
} }
[Fact]
public void SelectedItems_Indexer_Is_Correct()
{
// Issue #7974
var target = CreateTarget();
var raised = 0;
target.SelectionChanged += (s, e) =>
{
Assert.Equal("bar", e.SelectedItems.First());
Assert.Equal("bar", e.SelectedItems[0]);
++raised;
};
target.Select(1);
Assert.Equal(1, raised);
}
} }
public class BatchUpdate public class BatchUpdate

Loading…
Cancel
Save