When a ComboBox's data source is bound to an ImmutableArray, its value is reset when it loses focus.
Orace opened this issue · comments
.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.
@Olina-Zhang can your team please test?
Related StackOverflow post:
https://stackoverflow.com/questions/8940675/c-sharp-combobox-selectedvalue-gets-reset-on-focus-lost/
@elachlan
The issue can reproduce on both .NET Framework from 4.6 - 4.8.1 and .NET from 6.0 - 9.0.
@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.