fix: bind a field control to the view's current view model, so a view can bind before it has one by glennawatson · Pull Request #158 · reactiveui/ReactiveUI.Binding.SourceGenerators · GitHub
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

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

Copy link
Copy Markdown

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Oct 13, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant