gpt4 book ai didi

c# - 将多个组合框绑定(bind)到 1 个列表<>

转载 作者:太空狗 更新时间:2023-10-30 01:10:30 24 4
gpt4 key购买 nike

我正在尝试将多个组合框绑定(bind)到一个列表。但是,当我在 1 个组合框中选择一个值时,绑定(bind)到列表的所有组合框都会更改为相同的值。

List<Country> countryList = new List<Country>();
// Add contries to list. There are two properties string Name, and string Code
comboBox1.DataSource = countryList;
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "Code";


comboBox2.DataSource = countryList;
comboBox2.DisplayMember = "Name";
comboBox2.ValueMember = "Code";

现在当我从 comboBox1 选择一个国家时,它也会选择 comboBox2。我不想为每个 ComboBox 创建一个单独的列表。

最佳答案

抱歉,您必须创建一个新列表 。如果可以,您只需创建另一个列表即可。

comboBox2.DataSource = countryList.ToList();

但是,如果这对您不起作用,您可以为其中一个组合框创建一个单独的绑定(bind)上下文,然后这样做

        BindingContext bcG1 = new BindingContext();

comboBox1.BindingContext = bcG1;

comboBox1.DataSource = Countries;
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "Code";

comboBox2.DataSource = Countries;
comboBox2.DisplayMember = "Name";
comboBox2.ValueMember = "Code";

关于c# - 将多个组合框绑定(bind)到 1 个列表<>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4586481/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com