Skip to content

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

Open
@xackus

Description

@xackus

Describe the bug 🐞

When using BindCommand, the ViewModel passed to the withParameter expression can be different from the ViewModel the command is invoked on.

Step to reproduce

  1. Use a ReactiveUI.Winforms.ViewModelControlHost with CacheViews = true.
  2. Set a ViewModel.
  3. Set a new ViewModel of the same type.
  4. The command parameter is computed from the old ViewModel.

Reproduction repository

https://github.com/xackus/ReactiveUiBindCommandRepro

Expected behavior

The ViewModel passed to the withParameter expression is the same as the ViewModel the command is invoked on.

Screenshots 🖼️

No response

IDE

Visual Studio 2022

Operating system

Windows

Version

11

Device

PC

ReactiveUI Version

20.1.63

Additional information ℹ️

BindCommand internally uses Reflection.ViewModelWhenAnyValue for the subscription, which despite taking a ViewModel parameter, does not use it.
The only place where the ViewModel passed to BindCommand is actually used is when invoking the withParameter expression.

I find it very confusing that the Bind*(View, ViewModel, ...) family of functions seems to sometimes use the ViewModel that was passed in, and sometimes instead extracts the ViewModel from the View.
In my case I need the second behavior, because I reuse the View when possible. But then why pass in the ViewModel at all?
I haven't been using ReactiveUI for very long. Was the approach changed here some time ago?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions