dotnet / dotnet/winforms

Control Binding should use Invoke

Open
#8,532 18 comments 0 reactions 1 assignee Claimed by @KlausLoeffelmann View on GitHub
:construction: work in progress api-suggestion
Dominant language
C#
Stars
4.9k
Forks
1.1k
Avg merge
1d 13m
Merged PRs (30d)
85

Description

### Is your feature request related to a problem? Please describe

I have a WinForms application which uses CommunityToolkit.Mvvm for bindings.
I am also using the same MVVM library to build a MAUI application for study purposes.

MVVM allows me to have asynchronous commands and messages, which I use to propagate changes across models.
However, because my form binds some controls to those models, I have started to receive UI Thread exceptions when updating values.

After a lot of debugging, I saw that the `SetPropValue` method in the Bindings class does not use the `Invoke` method when updating values:
https://github.com/dotnet/winforms/blob/77d01dd234bc6fb2dac3b86d6c2a0b7cd61f1507/src/System.Windows.Forms/src/System/Windows/Forms/Binding.cs#L956

### Describe the solution you'd like and alternatives you've considered

It is my understanding that the `Control` property is available for the `Binding` object:
https://github.com/dotnet/winforms/blob/77d01dd234bc6fb2dac3b86d6c2a0b7cd61f1507/src/System.Windows.Forms/src/System/Windows/Forms/Binding.cs#L110

In this case, I propose that the `SetPropValue` method checks if the `Control` property is not null and tries to update it's value through an execution of the `Control.Invoke` method, to ensure the value change occurs in the UI Thread.

### Will this feature affect UI controls?

I do not think so because the underlining flow is kept as is.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.