dotnet / winforms

Windows Forms is a .NET UI framework for building Windows desktop applications.
MIT License
4.35k stars 965 forks source link

Data Bindings share object state. The main reason for choosing MVVM is the separation of concerns. #11722

Open egvijayanand opened 1 month ago

egvijayanand commented 1 month ago

.NET version

8.0.303

Did it work in .NET Framework?

Not tested/verified

Did it work in any of the earlier releases of .NET Core or .NET 5+?

No, I haven't tried in other versions.

Issue description

Data Bindings share object state. The main reason for choosing MVVM is the separation of concerns.

A ViewModel property can be bound to any # of controls, the underlying binding engine should ensure that the bound controls don't share state.

Whereas the WinForms binding engine shares state.

The binding logic works fine on all other UI frameworks where it's tested (.NET MAUI, WPF, Xamarin.Forms).

This needs to be addressed.

Steps to reproduce

Will upload the sample project to my GitHub repo and share the URL to clone.

Once cloned, have a look at the MainForm, hosting 2 ComboBox controls and both are data bound.

Then go to the MainForm.cs, and in the constructor, there will be two methods for the data binding configuration.

  1. CurrentBehavior() method - This will bind to the same ViewModel property, but it results in state-sharing
  2. ExpectedBehavior() method - This will bind to the different ViewModel properties so it works. Ideally, the first option should behave like this.

Comment the first method and uncomment the second method to know the difference.

Build and run the app with data bindings in different configurations.

To convey the issue, have created two different properties in ViewModel, and bound them to those two controls. This is an overhead.

egvijayanand commented 1 month ago

Here's the sample project to simulate this issue:

https://github.com/egvijayanand/winforms-issue-11722

egvijayanand commented 1 month ago

In the current behavior, any change in one ComboBox reflects on the other.

elachlan commented 1 month ago

@KlausLoeffelmann will be interest in feedback around databinding.