fix: bind a field control to the view's current view model, so a view can bind before it has one - #158
Merged
Conversation
… can bind before it has one A binding whose view side started at a field, such as a control named in XAML, bound the view model instance it was handed and threw when it was null. A field link's owner info now lists the view's ViewModel property, so the binding roots at the view and follows each view model it is given, as a binding onto a property already did.
glennawatson
force-pushed
the
fix/bind-follows-view-viewmodel
branch
from
September 28, 2026 17:06
9ec59c6 to
5b942d4
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #158 +/- ##
=======================================
Coverage 99.23% 99.23%
=======================================
Files 396 396
Lines 14458 14467 +9
Branches 2063 2065 +2
=======================================
+ Hits 14348 14357 +9
Misses 110 110 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Summary
A generated view binding onto a control the view holds in a field now follows the view's current view model, so a view can bind before it has one.
Bind,OneWayBind,BindCommandandBindInteractionread the view model through the view'sViewModelproperty when the view side starts at a field, such as a control named in XAML (v => v.NameBox.Text). They already did when it starts at a property.ViewModelis null starts once it is set, and follows each replacement, rather than throwingArgumentNullException.Why
A binding whose view side started at a field bound to the view model instance it was handed. A view that binds in its constructor or activation, before a router or host assigns its view model, got an
ArgumentNullExceptionfrom the generated binding. A view model assigned later, or replaced on navigation, was never observed. ReactiveUI binds to the view'sViewModelproperty, so these views worked there. The ReactiveUI.Avalonia example's command page showed it.Breaking changes
None for a view that sets its view model before binding. A binding onto a field control now observes the view's
ViewModelrather than the instance the call names, as a binding onto a property already did.How this was verified
Runtime tests bind one-way and two-way onto a field control while the view has no view model, then assign and replace it. The field-path command test now sets the view model, as ReactiveUI requires. Ran every
Documentation/Pagesexample,ToPropertyVerificationon net8.0 to net11.0,PlatformBindingsVerification -- --verifyon net10.0, and a native AOT publish ofsetup/aotwith no warnings.Notes for the reviewer
TypeDetectionExtractor.ExtractFieldOwneris the change: a field link's owner info now lists the view'sViewModelproperty, whichBindingEmitterHelpers.ResolveViewModelObservationlooks for to root the path at the view.Checklist