Skip to content

Picker Selected item gets cleared in BindableLayout - Fix - #26777

Closed
kubaflo wants to merge 1 commit into
dotnet:mainfrom
kubaflo:fix-25729
Closed

kubaflo wants to merge 1 commit into
dotnet:mainfrom
kubaflo:fix-25729

Conversation

@kubaflo

@kubaflo kubaflo commented Dec 23, 2024

Copy link
Copy Markdown
Contributor

Description of Change

Issues Fixed

Fixes #25729

Before After
Simulator.Screen.Recording.-.iPhone.16.Pro.Max.-.2024-12-23.at.03.31.53.mp4
Simulator.Screen.Recording.-.iPhone.16.Pro.Max.-.2024-12-23.at.03.20.47.mp4

@kubaflo
kubaflo requested a review from a team as a code owner December 23, 2024 02:34
@dotnet-policy-service dotnet-policy-service Bot added the community ✨ Community Contribution label Dec 23, 2024
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Hey there @kubaflo! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@PureWeen

Copy link
Copy Markdown
Member

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 3 pipeline(s).

@PureWeen PureWeen added this to the .NET 9 SR3 milestone Jan 5, 2025
@rmarinho

rmarinho commented Jan 6, 2025

Copy link
Copy Markdown
Member

Is it possible to add a test?

@kubaflo

kubaflo commented Jan 6, 2025

Copy link
Copy Markdown
Contributor Author

@rmarinho I've added a UiTest :)

@mattleibow
mattleibow requested a review from Copilot January 6, 2025 17:22

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 3 out of 4 changed files in this pull request and generated 1 comment.

Files not reviewed (1)
  • src/Controls/tests/TestCases.HostApp/Issues/Issue25729.xaml: Language not supported
Comments suppressed due to low confidence (1)

src/Controls/src/Core/Picker/Picker.cs:351

  • The new behavior in the ResetItems method needs to be covered by tests.
if (oldValue is null || (newValue?.Count ?? -1) < oldValue.Count)

Comment thread src/Controls/src/Core/Picker/Picker.cs Resolved
Comment thread src/Controls/src/Core/Picker/Picker.cs Outdated
Handler?.UpdateValue(nameof(IPicker.Items));

ClampSelectedIndex();
if (oldValue is null || (newValue?.Count ?? -1) < oldValue.Count)

@mattleibow mattleibow Jan 6, 2025 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like I am missing something here. (my brain is mush after new years) What was the cause of the issue that resulted in items being cleared? This appears to just make sure that the clamping only happens if there was no previous value or if there are less/no items in the new list.

Was there some ordering issue where it would always reset to -1 or something?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method 'ClampSelectedIndex()'

void ClampSelectedIndex()
{
	var oldIndex = SelectedIndex;
	var newIndex = SelectedIndex.Clamp(-1, Items.Count - 1);
	//FIXME use the specificity of the caller
	SetValue(SelectedIndexProperty, newIndex, SetterSpecificity.FromHandler);
	// If the index has not changed, still need to change the selected item
	if (newIndex == oldIndex)
		UpdateSelectedItem(newIndex);
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, but why does the clamping only need to happen sometimes? Or is it a bad-ish name and it is just for the SetValue line firing when it shouldn't? What happens now when the UpdateSelectedItem no longer fires? Will the SelectedItem not be updated, or will it happen later?

I am not sure I see how the "only update if it was null before" matters if the clamped value will end up in the same place. Assuming it was at pos -1 with a null list, we add a list and the pos is still -1. We change the list and the pos should still be -1. Or if it was pos 10 and the new list is 15 items, the clamped value should stay 10 and we would still want the updated SelectedItem.

@kubaflo kubaflo Jan 7, 2025 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is more like this:
When the binding is applied one more time, the ItemsCount is initially equal to 0 before it gets to its previous count and because of it the newIndex is set to -1

@mattleibow

Copy link
Copy Markdown
Member

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 3 pipeline(s).

@mattleibow mattleibow left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something strange is happening here. I made some changes to bind the selected index and it seems to clamp across VMs. For example, one picker has 3 items and the other has 4. Picking the last item in the 4, and then switching back to the 3 seems to update the VM to also be 3.

29b3f3c

Not sure if it is related...

Media1.mp4

@kubaflo

kubaflo commented Jan 7, 2025

Copy link
Copy Markdown
Contributor Author

Something strange is happening here. I made some changes to bind the selected index and it seems to clamp across VMs. For example, one picker has 3 items and the other has 4. Picking the last item in the 4, and then switching back to the 3 seems to update the VM to also be 3.

29b3f3c

Not sure if it is related...

Media1.mp4

I see that's not good. I'm trying to find another fix. I have a suspicion that the fix will involve SelectedIndex, because it is always set to 0 on iOS on context change

@mattleibow

mattleibow commented Jan 7, 2025 •

Copy link
Copy Markdown
Member

I just found out that this also happens with the selected ITEM. If you go from 4 items to 3 items, then going back to the 4 items results in the selection changing.

Media2.mp4

@kubaflo
kubaflo force-pushed the fix-25729 branch 2 times, most recently from 41fe294 to 5e235da Compare January 8, 2025 01:06
@kubaflo

kubaflo commented Jan 8, 2025

Copy link
Copy Markdown
Contributor Author

@mattleibow I've discovered a simpler fix. Basically there's no need for
Screenshot 2025-01-08 at 02 07 27
And this creates our unexpected behavior

Hi @BurningLights Is there a super important reason that I've missed, so that it cannot be removed?
#19728

@PureWeen PureWeen added this to the .NET 9 SR6 milestone Mar 7, 2025
@PureWeen PureWeen modified the milestones: .NET 9 SR6, .NET 9 SR7 Mar 23, 2025
@mahesh139

Copy link
Copy Markdown

any idea when this fix will be available? Google has SDK 35 requirement and we are stuck with Migration from Xamarin forms to MAUI due to this and couple of more issues.

@PureWeen PureWeen modified the milestones: .NET 9 Servicing, .NET 9 SR12 Aug 8, 2025
@PureWeen PureWeen moved this from Todo to Ready To Review in MAUI SDK Ongoing Aug 8, 2025
@PureWeen PureWeen modified the milestones: .NET 9 SR12, .NET 10 SR1 Sep 10, 2025
@PureWeen PureWeen modified the milestones: .NET 10 SR1, .NET 10.0 SR2 Nov 4, 2025
@PureWeen PureWeen modified the milestones: .NET 10.0 SR2, .NET 10 SR4 Dec 13, 2025
@PureWeen PureWeen modified the milestones: .NET 10.0 SR4, .NET 10 SR5 Jan 21, 2026
@kubaflo kubaflo closed this Mar 3, 2026
@github-project-automation github-project-automation Bot moved this from Ready To Review to Done in MAUI SDK Ongoing Mar 3, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators Apr 3, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Picker Selected item gets cleared in BindableLayout (Android, iOS) - Bug introduced in Microsoft.Maui.Controls 8.0.60

9 participants