dotnet / wpf

WPF is a .NET Core UI framework for building Windows desktop applications.
MIT License
7.08k stars 1.17k forks source link

ComboBox Binding Sequence Affecting Binding Ability of Other Controls #7909

Closed Alison-97 closed 1 year ago

Alison-97 commented 1 year ago

Description

The binding ability of the CheckBox control is affected by the sequence of ComboBox binding. If the ComboBox is binded first, the subsequent binding will not work.

https://github.com/dotnet/wpf/assets/97237245/df2788ca-42f5-4aff-8c74-1e1f194b5447

Reproduction Steps

  1. Open the sln file and run the WpfApp. Binding Sequence Issue.zip
  2. Click on Button, go to tabPage2.
  3. Toggle the Checkbox and to change the visibility of the Label in the GroupBox. Note that the checkbox is working as intended.
  4. Open the UserControl2.cs file.
  5. Go to Line 30 and move the BindComboBox line above of the two other bindings.
  6. Run the program again by repeating step 1-3.
  7. Note that the toggling of CheckBox is no longer working, where the Label is invisible regardlessly.

Note that when the GroupBox (containing the Label) and CheckBox is in tabPage1 instead of tabPage2 , the ComboBox binding sequence won’t affect the functionality of the CheckBox

  1. Go to Line 79 & 80 in UserControl2.Designer.cs.
  2. Replace the tabPage2 of groupBox and checkBox with tabPage1.
  3. Launch the program and the checkbox is working fine.

Expected behavior

The CheckBox should be working regardless of the sequence of the ComboBox binding

Actual behavior

The binding ability is dependent on the binding sequence, as well as the tabPage number

Regression?

No response

Known Workarounds

No response

Impact

No response

Configuration

No response

Other information

No response

miloush commented 1 year ago

These are all Windows Forms controls, I don't see how this belongs in this repo.

miloush commented 1 year ago

Note that you can reference Winforms in a supported way rather than adding its assemblies as references, and that is by checking a checkbox in the project properties:

image

Also note that BindingControlsToVM is called twice, once from constructor, once when loaded. If you call it only once in constructor, it seems to work for me regardless of the order.

Also note that you are promising with INotifyPropertyChanged but not delivering, you should be invoking PropertyChanged whenever IsChecked or DisableText values are changed.

However, I think you have more serious problems. I think in Winforms the binding does not apply until a control is made visible. Workarounds seem to suggest either calling internal CreateControl(true) on the controls, or making them visible in the designer and then hiding them in the code.

Why not make your app in WPF? It's so much better!

Alison-97 commented 1 year ago

These problematic controls are indeed from WinForms, but the binding issue only occurs when these WinForms controls are hosted in WPF. The attached file is a WPF project that incorporates the WinForms controls.

miloush commented 1 year ago

Not true: create a new Winforms app, add a button to it and on button click, add new UserControl2() to the Controls. Same behaviour.

WinFormsApp1.zip

Alison-97 commented 1 year ago

Thank you for pointing it out. The issue can indeed be reproduced even without using a WPF wrapper. Since the issue lies with only WinForms controls, I'll post it on the WinForms forum.