Skip to content

fix: bind a field control to the view's current view model, so a view can bind before it has one - #158

Merged
glennawatson merged 1 commit into
mainfrom
fix/bind-follows-view-viewmodel
Sep 28, 2026
Merged

glennawatson merged 1 commit into
mainfrom
fix/bind-follows-view-viewmodel

Conversation

@glennawatson

Copy link
Copy Markdown
Contributor

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, BindCommand and BindInteraction read the view model through the view's ViewModel property 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.
  • A binding made while the view's ViewModel is null starts once it is set, and follows each replacement, rather than throwing ArgumentNullException.

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 ArgumentNullException from the generated binding. A view model assigned later, or replaced on navigation, was never observed. ReactiveUI binds to the view's ViewModel property, 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 ViewModel rather 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/Pages example, ToPropertyVerification on net8.0 to net11.0, PlatformBindingsVerification -- --verify on net10.0, and a native AOT publish of setup/aot with no warnings.

Notes for the reviewer

TypeDetectionExtractor.ExtractFieldOwner is the change: a field link's owner info now lists the view's ViewModel property, which BindingEmitterHelpers.ResolveViewModelObservation looks for to root the path at the view.

Checklist

  • I have read the Contribute guide
  • The PR title follows Conventional Commits
  • Tests cover this change, or the summary says why they do not
  • New or changed public API has XML documentation

… 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
glennawatson force-pushed the fix/bind-follows-view-viewmodel branch from 9ec59c6 to 5b942d4 Compare September 28, 2026 17:06
@codecov

codecov Bot commented Sep 28, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.23%. Comparing base (64d1efa) to head (5b942d4).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@sonarqubecloud

Copy link
Copy Markdown

@glennawatson
glennawatson merged commit c2e9c2c into main Sep 28, 2026
23 checks passed
@glennawatson
glennawatson deleted the fix/bind-follows-view-viewmodel branch September 28, 2026 17:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant