Code Monkey home page Code Monkey logo

Comments (3)

ricardobossan avatar ricardobossan commented on July 17, 2024 1

@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.

from winforms.

elachlan avatar elachlan commented on July 17, 2024

@Olina-Zhang can your team please test?

Related StackOverflow post:
https://stackoverflow.com/questions/8940675/c-sharp-combobox-selectedvalue-gets-reset-on-focus-lost/

from winforms.

Liv-Goh avatar Liv-Goh commented on July 17, 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

from winforms.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.