@@ -401,20 +401,24 @@ public void TestItemsSourceCollectionChangedInsertBeforeSelected(int insertionIn
401401 ItemsSource = items ,
402402 SelectedIndex = 1
403403 } ;
404+ var originalSelectedItem = picker . SelectedItem ;
404405 items . InsertRange ( insertionIndex , insertNames . Select ( name => new { Name = name } ) ) ;
405406 Assert . Equal ( 3 + insertNames . Length , picker . Items . Count ) ;
406- Assert . Equal ( 1 , picker . SelectedIndex ) ;
407- Assert . Equal ( items [ 1 ] , picker . SelectedItem ) ;
407+ // The selected item should remain the same, but the index should update
408+ Assert . Equal ( originalSelectedItem , picker . SelectedItem ) ;
409+ Assert . Equal ( items . IndexOf ( originalSelectedItem ) , picker . SelectedIndex ) ;
408410 }
409411
410412 [ Theory ]
411- [ InlineData ( 0 , 1 ) ]
412- [ InlineData ( 1 , 1 ) ]
413- [ InlineData ( 2 , 1 ) ]
414- [ InlineData ( 0 , 2 ) ]
415- [ InlineData ( 1 , 2 ) ]
416- [ InlineData ( 2 , 2 ) ]
417- public void TestItemsSourceCollectionChangedRemoveBeforeSelected ( int removeIndex , int removeCount )
413+ // Cases where removed items do NOT include the selected item (Paul at index 1)
414+ [ InlineData ( 0 , 1 , true ) ] // Remove John - Paul should be preserved
415+ [ InlineData ( 2 , 1 , true ) ] // Remove Ringo - Paul should be preserved
416+ [ InlineData ( 2 , 2 , true ) ] // Remove Ringo and George - Paul should be preserved
417+ // Cases where removed items include the selected item
418+ [ InlineData ( 1 , 1 , false ) ] // Remove Paul - selection changes
419+ [ InlineData ( 0 , 2 , false ) ] // Remove John and Paul - selection changes
420+ [ InlineData ( 1 , 2 , false ) ] // Remove Paul and Ringo - selection changes
421+ public void TestItemsSourceCollectionChangedRemoveBeforeSelected ( int removeIndex , int removeCount , bool selectedItemPreserved )
418422 {
419423 var items = new ObservableRangeCollection < object >
420424 {
@@ -429,11 +433,21 @@ public void TestItemsSourceCollectionChangedRemoveBeforeSelected(int removeIndex
429433 ItemsSource = items ,
430434 SelectedIndex = 1
431435 } ;
436+ var originalSelectedItem = picker . SelectedItem ;
432437 items . RemoveRange ( removeIndex , removeCount ) ;
433438
434439 Assert . Equal ( 4 - removeCount , picker . Items . Count ) ;
435- Assert . Equal ( 1 , picker . SelectedIndex ) ;
436- Assert . Equal ( items [ 1 ] , picker . SelectedItem ) ;
440+ if ( selectedItemPreserved )
441+ {
442+ // The selected item should remain the same, but the index should update
443+ Assert . Equal ( originalSelectedItem , picker . SelectedItem ) ;
444+ Assert . Equal ( items . IndexOf ( originalSelectedItem ) , picker . SelectedIndex ) ;
445+ }
446+ else
447+ {
448+ // The selected item was removed, so a different item is now selected
449+ Assert . NotEqual ( originalSelectedItem , picker . SelectedItem ) ;
450+ }
437451 }
438452
439453 [ Theory ]
@@ -792,5 +806,51 @@ public void NullItemReturnsEmptyStringFromInterface()
792806 var thing = ( picker as IPicker ) . GetItem ( 0 ) ;
793807 Assert . NotNull ( thing ) ;
794808 }
809+
810+ // https://github.com/dotnet/maui/issues/29235
811+ [ Fact ]
812+ public void PickerPreservesSelectedItemAfterRemovingItemBeforeSelection ( )
813+ {
814+ // Arrange: Create a picker with items and select "Item2" (index 2)
815+ var items = new ObservableCollection < string > { "Item0" , "Item1" , "Item2" } ;
816+ var picker = new Picker
817+ {
818+ ItemsSource = items ,
819+ SelectedIndex = 2 // Select "Item2"
820+ } ;
821+
822+ Assert . Equal ( "Item2" , picker . SelectedItem ) ;
823+ Assert . Equal ( 2 , picker . SelectedIndex ) ;
824+
825+ // Act: Remove the first item (before the selection)
826+ items . RemoveAt ( 0 ) ;
827+
828+ // Assert: SelectedItem should still be "Item2", but index should now be 1
829+ Assert . Equal ( "Item2" , picker . SelectedItem ) ;
830+ Assert . Equal ( 1 , picker . SelectedIndex ) ;
831+ }
832+
833+ // https://github.com/dotnet/maui/issues/29235
834+ [ Fact ]
835+ public void PickerPreservesSelectedItemAfterInsertingItemBeforeSelection ( )
836+ {
837+ // Arrange: Create a picker with items and select "Dog" (index 1)
838+ var items = new ObservableCollection < string > { "Cat" , "Dog" , "Rabbit" } ;
839+ var picker = new Picker
840+ {
841+ ItemsSource = items ,
842+ SelectedIndex = 1 // Select "Dog"
843+ } ;
844+
845+ Assert . Equal ( "Dog" , picker . SelectedItem ) ;
846+ Assert . Equal ( 1 , picker . SelectedIndex ) ;
847+
848+ // Act: Insert an item at the beginning
849+ items . Insert ( 0 , "Goat" ) ;
850+
851+ // Assert: SelectedItem should still be "Dog", and index should now be 2
852+ Assert . Equal ( "Dog" , picker . SelectedItem ) ;
853+ Assert . Equal ( 2 , picker . SelectedIndex ) ;
854+ }
795855 }
796856}
0 commit comments