Repository navigation
[net11] Picker: don't write the selection into the previous BindingContext - #41328
wbv-profile wants to merge 21 commits into
Conversation
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
|
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. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
MAUI Autopilot — StartedModels
Info
|
There was a problem hiding this comment.
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.
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>
There was a problem hiding this comment.
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.
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>
There was a problem hiding this comment.
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.
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>
There was a problem hiding this comment.
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.
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>
There was a problem hiding this comment.
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.
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>
Target Branch
mainnet11, for reviewFor .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
Pickergets a newBindingContext,BindableObject.ApplyBindingsre-applies its bindings one at a time. Until a binding is re-applied, it still points at the previous context.ItemsSourcerunsResetItems, which reconciles the selection immediately withClampSelectedIndex(SelectedIndex).SelectedIndexis still the previous context's index, soSelectedItembecomes the new list's item at that index, ornull.SetValueActualpushes that change through the TwoWaySelectedItembinding, 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 bynull.SelectedItemis looked up in the previous context's items. It isn't found, soSelectedIndexbecomes -1 andSelectedItemis set tonull, 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 theBindingContextsetter andSetInheritedBindingContext), it sets aprivate protectedIsApplyingBindingContextflag. When all bindings point at the new context, it calls a newprivate protected virtual OnBindingContextBindingsApplied(), beforeOnBindingContextChanged(). Neither member is public, so there is no public API change, and nothing else uses them.Picker: whileIsApplyingBindingContextis set, it records which of items,SelectedItemandSelectedIndexchanged, without reconciling them. InOnBindingContextBindingsAppliedit reconciles once, against the new context:SelectedItemchanged, it runs the existingUpdateSelectedIndex(SelectedItem);SelectedIndexchanged, it runsUpdateSelectedItem(SelectedIndex);ResetItemslogic (moved unchanged intoReconcileSelectionWithItems).Outside a binding-context change, every path behaves exactly as before.
SelectedIndexChangedstill 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 throughapple, banana, none, apple, banana, none, banana, apple. After each switch,SelectedItemandSelectedIndexmust 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):
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.PickerTests+PickerSelectedIndexChangedLoopTests: 70/70 passed.Controls.Core.UnitTests: 6874 total, 6844 passed, 30 skipped, 0 failed.Controls.Xaml.UnitTests(afterdotnet build Microsoft.Maui.BuildTasks.slnf): 2303 total, 2295 passed, 8 skipped, 0 failed.Risk. Low to moderate.
BindableObjectgains one bool and a no-op virtual call per binding-context change.Pickerchanges behaviour, and only while its bindings are being re-applied.SelectedIndexstill has the previous value. It is updated as soon as the bindings are applied, in the same call.SelectedItemisn'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
BindingExpressionbehaviour, not specific toPicker.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