Skip to content

Commit 2747773

Browse files
fix(reg): Fix SelectedIndex update when ItemsSource is ObservableCollection
Fix regression introduced because we were 'double dipping' the collection changed event, both directly on the ItemsSource and via ItemsControl.Items.
1 parent 6471ab2 commit 2747773

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Controls/Given_ListViewBase_Items.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,33 @@ public void When_ItemsSource_Grouped_Observables_Outer_Modified()
413413
#endif
414414
}
415415

416+
[TestMethod]
417+
public void When_ItemsSource_ObservableCollection_Selection()
418+
{
419+
var itemsSource = new ObservableCollection<string>(Enumerable.Range(0, 80).Select(i => $"Item {i}"));
420+
var SUT = new ListView { ItemsSource = itemsSource };
421+
422+
SUT.SelectedIndex = 5;
423+
424+
Assert.AreEqual(5, SUT.SelectedIndex);
425+
Assert.AreEqual("Item 5", SUT.SelectedItem);
426+
427+
itemsSource.RemoveAt(2);
428+
429+
Assert.AreEqual(4, SUT.SelectedIndex);
430+
Assert.AreEqual("Item 5", SUT.SelectedItem);
431+
432+
itemsSource.RemoveAt(22);
433+
434+
Assert.AreEqual(4, SUT.SelectedIndex);
435+
Assert.AreEqual("Item 5", SUT.SelectedItem);
436+
437+
itemsSource.Insert(1, "Item 1.5");
438+
439+
Assert.AreEqual(5, SUT.SelectedIndex);
440+
Assert.AreEqual("Item 5", SUT.SelectedItem);
441+
}
442+
416443
private static ObservableCollection<GroupingObservableCollection<TKey, TElement>> GetGroupedObservable<TKey, TElement>(IEnumerable<TElement> source, Func<TElement, TKey> keySelector)
417444
{
418445
var observables = source.GroupBy(keySelector).Select(g => new GroupingObservableCollection<TKey, TElement>(g.Key, g));

src/Uno.UI/UI/Xaml/Controls/Primitives/Selector.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -507,10 +507,14 @@ internal virtual void OnItemClicked(int clickedIndex) { }
507507

508508
protected override void OnItemsChanged(object e)
509509
{
510-
if (e is IVectorChangedEventArgs iVCE)
510+
if (
511+
// When ItemsSource is set, we get collection changes from it directly (and it's not possible to directly modify Items)
512+
ItemsSource == null &&
513+
e is IVectorChangedEventArgs iVCE
514+
)
511515
{
512516
if (iVCE.CollectionChange == CollectionChange.ItemChanged
513-
|| (iVCE.CollectionChange == CollectionChange.ItemInserted && iVCE.Index < Items.Count))
517+
|| (iVCE.CollectionChange == CollectionChange.ItemInserted && iVCE.Index < Items.Count))
514518
{
515519
var item = Items[(int)iVCE.Index];
516520

@@ -624,7 +628,7 @@ protected void SetFocusedItem(int index,
624628
FocusState focusState,
625629
bool animateIfBringIntoView)
626630
{
627-
631+
628632
}
629633

630634
protected void SetFocusedItem(int index,
@@ -711,7 +715,7 @@ protected void SetFocusedItem(int index,
711715

712716
private void ScrollIntoView(int index, bool isGroupItemIndex, bool isHeader, bool isFooter, bool isFromPublicAPI, bool ensureContainerRealized, bool animateIfBringIntoView, ScrollIntoViewAlignment @default)
713717
{
714-
718+
715719
}
716720

717721
protected void SetFocusedItem(int index,
@@ -748,7 +752,7 @@ protected void SetFocusedItem(int index,
748752
protected void SetFocusedItem(int index,
749753
bool shouldScrollIntoView)
750754
{
751-
755+
752756
}
753757

754758
bool CanScrollIntoView()

0 commit comments

Comments
 (0)