Skip to content

[net11] Picker: don't write the selection into the previous BindingContext - #41328

Open
wbv-profile wants to merge 21 commits into
dotnet:net11from
wbv-profile:fix/41308-picker-bindingcontext-selecteditem
Open

wbv-profile wants to merge 21 commits into
dotnet:net11from
wbv-profile:fix/41308-picker-bindingcontext-selecteditem

Conversation

@wbv-profile

Copy link
Copy Markdown
Contributor

Target Branch

This PR is for Initial base
New features, API changes, or .NET 12 work (including .NET 12 fixes) main
.NET 11 servicing fixes net11, for review
Older servicing, release integration, infrastructure, or documentation The branch designated for that work

For .NET 11 fixes, only a maintainer retargets the approved PR to the active
inflight lane and merges it after CI validates that final base. Do not merge
individual fixes directly into net11; agents must not retarget or merge.

Description of Change

Root cause. When a Picker gets a new BindingContext, BindableObject.ApplyBindings re-applies its bindings one at a time. Until a binding is re-applied, it still points at the previous context.

  • ItemsSource bound before SelectedItem (the repro in Picker writes null into the previous/new BindingContext's SelectedItem (TwoWay) when its BindingContext changes, wiping user selections #41308): re-applying ItemsSource runs ResetItems, which reconciles the selection immediately with ClampSelectedIndex(SelectedIndex). SelectedIndex is still the previous context's index, so SelectedItem becomes the new list's item at that index, or null. SetValueActual pushes that change through the TwoWay SelectedItem binding, which hasn't been re-applied yet, so it is written into the previous view model. The selection the user made there is replaced by another list's item or by null.
  • SelectedItem bound before ItemsSource (XAML attribute order): the new context's SelectedItem is looked up in the previous context's items. It isn't found, so SelectedIndex becomes -1 and SelectedItem is set to null, which is written into the new view model.

Which selections are lost depends on the item counts and the order the contexts are visited, which is why the results differ between 10.0.x and 11.0.x in the issue. The current net11 (with #39971) still fails.

Change.

  • BindableObject: while it re-applies its bindings to a new context (both the BindingContext setter and SetInheritedBindingContext), it sets a private protected IsApplyingBindingContext flag. When all bindings point at the new context, it calls a new private protected virtual OnBindingContextBindingsApplied(), before OnBindingContextChanged(). Neither member is public, so there is no public API change, and nothing else uses them.

  • Picker: while IsApplyingBindingContext is set, it records which of items, SelectedItem and SelectedIndex changed, without reconciling them. In OnBindingContextBindingsApplied it reconciles once, against the new context:

    • if SelectedItem changed, it runs the existing UpdateSelectedIndex(SelectedItem);
    • else, if SelectedIndex changed, it runs UpdateSelectedItem(SelectedIndex);
    • else, if only the items changed, it runs the previous ResetItems logic (moved unchanged into ReconcileSelectionWithItems).

    Outside a binding-context change, every path behaves exactly as before. SelectedIndexChanged still fires when the property changes.

Tests (src/Controls/tests/Core.UnitTests/PickerTests.cs):

  • ChangingBindingContextDoesNotWriteSelectionIntoViewModels(bindSelectedItemFirst: false/true): the issue's repro, extended. Three view models, each with its own item instances: Apple selected, Banana selected, and none. The Picker is re-bound through apple, banana, none, apple, banana, none, banana, apple. After each switch, SelectedItem and SelectedIndex must match the current view model. At the end, no view model may have received a write that changed its selection. Both binding orders are covered.
  • SelectionStillFlowsToViewModelAfterBindingContextChanges: after context switches, a user selection (SelectedIndex = 2, then -1) still reaches the current view model, and only that one.

How it was verified (Windows, SDK 11.0.100, no workloads):

dotnet build src/Controls/tests/Core.UnitTests/Controls.Core.UnitTests.csproj -p:IncludeIosTargetFrameworks=false -p:IncludeMacCatalystTargetFrameworks=false -p:IncludeMacOSTargetFrameworks=false -p:IncludeAndroidTargetFrameworks=false -p:IncludeWindowsTargetFrameworks=false
artifacts/bin/Controls.Core.UnitTests/Debug/net11.0/Microsoft.Maui.Controls.Core.UnitTests.exe --filter-class Microsoft.Maui.Controls.Core.UnitTests.PickerTests
  • Without the change (net11 @ 685476e): all 3 new tests fail. For example, Expected: "Banana" Actual: null, and a view model ends up holding another view model's "Cherry" instance.
  • With the change: PickerTests + PickerSelectedIndexChangedLoopTests: 70/70 passed.
  • Full Controls.Core.UnitTests: 6874 total, 6844 passed, 30 skipped, 0 failed.
  • Full Controls.Xaml.UnitTests (after dotnet build Microsoft.Maui.BuildTasks.slnf): 2303 total, 2295 passed, 8 skipped, 0 failed.

Risk. Low to moderate.

  • BindableObject gains one bool and a no-op virtual call per binding-context change.
  • Only Picker changes behaviour, and only while its bindings are being re-applied.
  • During that window, the native picker can briefly receive the new items while SelectedIndex still has the previous value. It is updated as soon as the bindings are applied, in the same call.
  • Unchanged edge case: when the new context's SelectedItem isn't in its own items, the selection is cleared, as it was before.

Not changed. A TwoWay binding still writes the value it has just applied back to its source (the same value, so no selection changes). That is generic BindingExpression behaviour, not specific to Picker.

Related. #26532 (same defect, closed); #25842 (reported in #41308 as the same root cause, triggered by CollectionView cell recycling; I haven't verified that scenario separately); #25729 and #26777 (closed). The binding-order cause was first traced in the discussion on #26777.

Issues Fixed

Fixes #41308

Let AI finish this PR

  • Let the MAUI agent address review feedback and fix remaining issues on this PR. Maintainers still decide whether it is merged. For PRs from forks, the agent opens a follow-up PR that keeps your commits.

When a Picker gets a new BindingContext, its bindings are re-applied one
at a time. Re-applying ItemsSource reconciled the selection right away
(ResetItems -> ClampSelectedIndex with the old SelectedIndex), and the
resulting SelectedItem change was pushed back through the TwoWay
SelectedItem binding, which still pointed at the previous context. A
selection the user made in one view model was overwritten with another
list's item or with null. With SelectedItem bound before ItemsSource, the
new context's SelectedItem was looked up in the old items, and the
resulting null was written into the new view model.

BindableObject now records while it re-applies its bindings to a new
context and calls a private protected hook once they all point at it.
Picker defers selection reconciliation (items, SelectedItem and
SelectedIndex changes) until then, and reconciles once against the new
context's items.

Fixes dotnet#41308
@dotnet-policy-service dotnet-policy-service Bot added the community ✨ Community Contribution label Dec 5, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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

@azure-pipelines

This comment has been minimized.

@kubaflo

This comment has been minimized.

@github-actions github-actions Bot added the s/autopilot-running MAUI Autopilot loop is running; remove this label to stop it label Dec 5, 2026
@github-actions

github-actions Bot commented Dec 5, 2026

Copy link
Copy Markdown
Contributor

MAUI Autopilot — Started

Models
  • Review: GPT-6.1 Sol (MAUI expert reviewer)
  • Fix: GPT-6.2 Sol and MAI Code 2 fix independently, then cross-examine each other; the best fix is kept until the review is clean and relevant tests pass
  • Test selection: GPT-6.2 Sol and MAI Code 2
Info
Label Meaning
s/autopilot-reviewing The MAUI expert reviewer is reviewing the PR
s/autopilot-fixing Fixing review findings or failing tests
s/autopilot-testing Running the relevant UI and device tests
s/autopilot-waiting-for-maintainer-review Review clean and tests passed
s/autopilot-needs-maintainer-decision The loop stopped; a maintainer decides how to continue

@github-actions github-actions Bot added the s/autopilot-reviewing MAUI Autopilot: MAUI expert review in progress label Dec 5, 2026

@github-actions github-actions Bot 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.

Expert Review — 4 findings · ⚠️ 1 breaking change

Reviewed commit 93d9b91.

Warning

Existing apps would look or behave differently without opting in. See the Behavioral Change Disclosure findings at:

See inline comments for details.

Comment thread src/Controls/src/Core/Picker/Picker.cs Resolved
Comment thread src/Controls/src/Core/Picker/Picker.cs Resolved
Comment thread src/Controls/src/Core/Picker/Picker.cs Resolved
Comment thread src/Controls/src/Core/Picker/Picker.cs Resolved
@github-actions github-actions Bot added s/autopilot-fixing MAUI Autopilot: fixing review findings or failing tests (GPT-6.2 Sol vs MAI Code 2) and removed s/autopilot-reviewing MAUI Autopilot: MAUI expert review in progress labels Dec 5, 2026
github-actions Bot and others added 4 commits December 5, 2026 08:50
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@github-actions github-actions Bot added s/autopilot-reviewing MAUI Autopilot: MAUI expert review in progress and removed s/autopilot-fixing MAUI Autopilot: fixing review findings or failing tests (GPT-6.2 Sol vs MAI Code 2) labels Dec 5, 2026

@github-actions github-actions Bot 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.

Expert Review — 6 findings · ⚠️ 2 breaking changes

Reviewed commit 2969556.

Warning

Existing apps would look or behave differently without opting in. See the Behavioral Change Disclosure findings at:

See inline comments for details.

Comment thread src/Controls/src/Core/Picker/Picker.cs Resolved
Comment thread src/Controls/src/Core/Picker/Picker.cs Resolved
Comment thread src/Controls/src/Core/Picker/Picker.cs Resolved
Comment thread src/Controls/src/Core/Picker/Picker.cs Resolved
Comment thread src/Controls/src/Core/Picker/Picker.cs Resolved
Comment thread src/Controls/src/Core/BindableObject.cs Resolved
@github-actions github-actions Bot added s/autopilot-fixing MAUI Autopilot: fixing review findings or failing tests (GPT-6.2 Sol vs MAI Code 2) and removed s/autopilot-reviewing MAUI Autopilot: MAUI expert review in progress labels Dec 5, 2026
github-actions Bot and others added 6 commits December 5, 2026 09:26
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@github-actions github-actions Bot added s/autopilot-reviewing MAUI Autopilot: MAUI expert review in progress and removed s/autopilot-fixing MAUI Autopilot: fixing review findings or failing tests (GPT-6.2 Sol vs MAI Code 2) labels Dec 5, 2026

@github-actions github-actions Bot 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.

Expert Review — 3 findings · ⚠️ 1 breaking change

Reviewed commit 67ba103.

Warning

Existing apps would look or behave differently without opting in. See the Behavioral Change Disclosure findings at:

See inline comments for details.

Comment thread src/Controls/src/Core/Picker/Picker.cs Resolved
Comment thread src/Controls/src/Core/Picker/Picker.cs Resolved
@github-actions github-actions Bot removed the s/autopilot-reviewing MAUI Autopilot: MAUI expert review in progress label Dec 5, 2026
@github-actions github-actions Bot added the s/autopilot-fixing MAUI Autopilot: fixing review findings or failing tests (GPT-6.2 Sol vs MAI Code 2) label Dec 5, 2026
github-actions Bot and others added 3 commits December 5, 2026 10:22
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@github-actions github-actions Bot added s/autopilot-reviewing MAUI Autopilot: MAUI expert review in progress and removed s/autopilot-fixing MAUI Autopilot: fixing review findings or failing tests (GPT-6.2 Sol vs MAI Code 2) labels Dec 5, 2026

@github-actions github-actions Bot 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.

Expert Review — 3 findings · ⚠️ 1 breaking change

Reviewed commit d718ffe.

Warning

Existing apps would look or behave differently without opting in. See the Behavioral Change Disclosure findings at:

See inline comments for details.

Comment thread src/Controls/src/Core/Picker/Picker.cs Resolved
Comment thread src/Controls/src/Core/Picker/Picker.cs Resolved
Comment thread src/Controls/src/Core/Picker/Picker.cs Resolved
@github-actions github-actions Bot added s/autopilot-fixing MAUI Autopilot: fixing review findings or failing tests (GPT-6.2 Sol vs MAI Code 2) and removed s/autopilot-reviewing MAUI Autopilot: MAUI expert review in progress labels Dec 5, 2026
github-actions Bot and others added 3 commits December 5, 2026 11:04
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@github-actions github-actions Bot added s/autopilot-reviewing MAUI Autopilot: MAUI expert review in progress and removed s/autopilot-fixing MAUI Autopilot: fixing review findings or failing tests (GPT-6.2 Sol vs MAI Code 2) labels Dec 5, 2026

@github-actions github-actions Bot 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.

Expert Review — 4 findings · ⚠️ 2 breaking changes

Reviewed commit 0bc9407.

Warning

Existing apps would look or behave differently without opting in. See the Behavioral Change Disclosure findings at:

See inline comments for details.

Comment thread src/Controls/src/Core/Picker/Picker.cs Resolved
Comment thread src/Controls/src/Core/Picker/Picker.cs Resolved
Comment thread src/Controls/src/Core/Picker/Picker.cs Resolved
Comment thread src/Controls/src/Core/Picker/Picker.cs Resolved
@github-actions github-actions Bot added s/autopilot-fixing MAUI Autopilot: fixing review findings or failing tests (GPT-6.2 Sol vs MAI Code 2) and removed s/autopilot-reviewing MAUI Autopilot: MAUI expert review in progress labels Dec 5, 2026
github-actions Bot and others added 4 commits December 5, 2026 11:53
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@github-actions github-actions Bot added s/autopilot-reviewing MAUI Autopilot: MAUI expert review in progress and removed s/autopilot-fixing MAUI Autopilot: fixing review findings or failing tests (GPT-6.2 Sol vs MAI Code 2) labels Dec 5, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community ✨ Community Contribution s/autopilot-reviewing MAUI Autopilot: MAUI expert review in progress s/autopilot-running MAUI Autopilot loop is running; remove this label to stop it

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants