Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When a ComboBox's data source is bound to an ImmutableArray, its value is reset when it loses focus. #11602

Open
Orace opened this issue Jun 27, 2024 · 3 comments
Assignees
Labels
area-Binding Impacting DataBinding untriaged The team needs to look at this issue in the next triage

Comments

@Orace
Copy link

Orace commented Jun 27, 2024

.NET version

net8.0-windows

Did it work in .NET Framework?

No

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

not tested

Issue description

When a ComboBox is bound to a data source of type ImmutableArray, the selected value resets to the initial value upon losing focus.

This issue does not occur if the ImmutableArray is exposed as an IReadOnlyList.

This issue also occur in .Net 4.8.1 with the System.Collections.Immutable v8.0.0 package.

A minimal reproducible example is available here.

Steps to reproduce

  • Download the minimal reproducible example
  • Compile and start the project.
  • Chose the second item in the As ImmutableArray combo box.
  • Click on Dummy button (so the combo box lost focus)

At this point the first item is selected back.

@Orace Orace added the untriaged The team needs to look at this issue in the next triage label Jun 27, 2024
@elachlan
Copy link
Contributor

elachlan commented Jun 27, 2024

@elachlan elachlan added the area-Binding Impacting DataBinding label Jun 27, 2024
@Liv-Goh
Copy link

Liv-Goh commented Jun 28, 2024

@elachlan
The issue can reproduce on both .NET Framework from 4.6 - 4.8.1 and .NET from 6.0 - 9.0.
combo box immutable array issue

@ricardobossan ricardobossan self-assigned this Jul 3, 2024
@ricardobossan
Copy link
Contributor

@Tanya-Solyanik The core of the problem lies in the immutable nature of ImmutableArray. WinForms data binding mechanisms require collections that support change notifications to properly propagate changes between the UI and the data source. ImmutableArray does not support these notifications, leading to the observed behavior.

Also, defining Value1 as IReadOnlyList, as it is done in the minimal reproducible example provided by the author, might give the illusion of immutability, but due to its access level being set to public, it can be modified from outside the class, which is not the case with ImmutableArray.

Workaround:

For data binding scenarios in WinForms, it is advisable to use ObservableCollection or BindingList. These collections are mutable and, therefore, support the two-way data binding required by WinForms.

Here is a modified snippet of MainModel.cs, from the sample code, demonstrating how to adapt the existing code to use ObservableCollection<int> instead of ImmutableArray for Values2:

public ObservableCollection<int> Values2 { get; }

public MainModel()
{
    var values = new[] { 1, 2, 3 };
    Values1 = values.ToImmutableArray();
    Values2 = new ObservableCollection<int>(values);
}

This approach ensures that the ComboBox can properly interact with the data source and maintain its state across UI interactions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-Binding Impacting DataBinding untriaged The team needs to look at this issue in the next triage
Projects
None yet
Development

No branches or pull requests

4 participants