Skip to content

Fix BindCommand passing incorrect parameter after assigning new ViewModel to View#4324

Open
xackus wants to merge 4 commits intoreactiveui:mainfrom
xackus:fix-command-parameter-after-vm-reassignment
Open

Fix BindCommand passing incorrect parameter after assigning new ViewModel to View#4324
xackus wants to merge 4 commits intoreactiveui:mainfrom
xackus:fix-command-parameter-after-vm-reassignment

Conversation

@xackus
Copy link
Copy Markdown

@xackus xackus commented Apr 3, 2026

What kind of change does this PR introduce?

Fixes #3970.

What is the current behavior?

The command parameter is derived from the original ViewModel passed into BindCommand.

What is the new behavior?

The command parameter is derived from the current ViewModel (IViewFor.ViewModel).

What might this PR break?

Theoretically this could break code that relies on the stale command parameter value.

Please check if the PR fulfills these requirements

  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)

Other information:

{
ArgumentExceptionHelper.ThrowIfNull(vmProperty);
ArgumentExceptionHelper.ThrowIfNull(controlProperty);
ArgumentExceptionHelper.ThrowIfNull(withParameter);
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before this change null was rejected by withParameter.ToObservable(viewModel) anyway.

@xackus
Copy link
Copy Markdown
Author

xackus commented Apr 3, 2026

I found dead code that did the correct thing. I think this was regressed by #2766.

@glennawatson
Copy link
Copy Markdown
Contributor

Thanks for the PR. Will review over the next day or so. Won't be straight away.

From the quick glance so far looks good though.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a BindCommand inconsistency where the command parameter was being computed from the originally supplied ViewModel instead of the view’s current IViewFor.ViewModel, which shows up when reusing views and reassigning ViewModels.

Changes:

  • Update CommandBinderImplementation.BindCommand(..., Expression withParameter, ...) to derive the parameter observable via Reflection.ViewModelWhenAnyValue(...) (i.e., from the current view.ViewModel).
  • Remove an internal mixin overload that previously built the parameter observable from the initially passed ViewModel.
  • Add a WPF regression test and update WinForms tests to call the binder implementation directly.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
src/ReactiveUI/Bindings/Command/CommandBinderImplementation.cs Computes command parameter from the current view.ViewModel instead of the originally passed ViewModel.
src/ReactiveUI/Bindings/Command/CommandBinderImplementationMixins.cs Removes the internal overload that created a stale parameter observable from the initially supplied ViewModel.
src/tests/ReactiveUI.Wpf.Tests/Wpf/WpfCommandBindingImplementationTests.cs Adds a regression test for parameter correctness after ViewModel reassignment.
src/tests/ReactiveUI.WinForms.Tests/winforms/CommandBindingImplementationTests.cs Updates tests to call BindCommand on the binder implementation instance (no longer using the removed mixin overload).

@glennawatson
Copy link
Copy Markdown
Contributor

They @xackus if you feel up to it would appreciate it you can evaluate copilot comments. It definitely gets things wrong at times so feel free to write a comment and resolve

Sometimes it has some good suggestions though.

Just a public holiday here so visiting family so limited availability.

@xackus
Copy link
Copy Markdown
Author

xackus commented Apr 4, 2026

Copilot helped uncover another bug by pointing out an issue in my test.
A WPF specific command binding optimization was being applied too aggressively.

@xackus xackus force-pushed the fix-command-parameter-after-vm-reassignment branch from 49d030b to 659cfab Compare April 4, 2026 15:10
@xackus
Copy link
Copy Markdown
Author

xackus commented Apr 4, 2026

There were some copies of the docs that were wrong / out of date.
I didn't really change anything in the documentation, just stitched together a single up to date version, pasted it everywhere, improved formatting, and added a note that the ViewModel parameter is there for type inference.

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.

[Bug]: Inconsistent BindCommand behaviour when reusing a View with a new ViewModel

3 participants