将多个 ComboBox 绑定到单个列表 - 问题:当我选择一个项目时,所有组合框都会更改

Bind multiple ComboBox to a single List - Issue: When I choose an item, all combo boxes change(将多个 ComboBox 绑定到单个列表 - 问题:当我选择一个项目时,所有组合框都会更改)

本文介绍了将多个 ComboBox 绑定到单个列表 - 问题:当我选择一个项目时,所有组合框都会更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在动态创建一个 ComboBox 数组,所有 ComboBoxDataSource 是一个包含一些整数的整数列表.但是当我在任何一个组合框中更改一个值说 X 时,所有其他组合值都会重置为值 X.

I am creating a ComboBox array dynamically and the DataSource for all the ComboBox is a single integer list that contains some integers. But when I change a value say X in any one combo box then all other combo values get reset to value X.

所以情况如下:

  • 所有组合框控件都绑定到一个列表
  • 当我更改组合框的选定项时,所有其他组合框控件的选定项也会更改.

我怎样才能阻止这些行为?

How can I stop these behavior?

推荐答案

由于您将所有组合框绑定到同一个数据源 - 单个列表 - 它们使用单​​个 BindingManagerBase.

Since you are binding all combo boxes to the same data source - a single list - they are using a single BindingManagerBase.

因此,当您从组合框中选择一个项目时,当前的 Position 的共享绑定管理器基本更改,所有组合框都转到该位置他们共享的数据源.

So when you choose an item from one of combo boxes, the current Position of the shared binding manager base changes and all combo boxes goes to that position of their shared data source.

为了解决这个问题,你可以将它们绑定到不同的数据源:

To solve the problem you can bind them to different data source:

  • 您可以将它们绑定到 yourList.ToList() 或任何其他列表,例如不同的 BindingList<T>.

  • You can bind them to yourList.ToList() or any other list for example different BindingList<T>.

combo1.DataSource = yourList.ToList();
combo2.DataSource = yourList.ToList();

  • 您可以为它们使用不同的 BindingSource 并将您的列表设置为 BindingSource 的 DataSource

  • You can use different BindingSource for them and set your list as DataSource of BindingSource

    combo1.DataSource = new BindingSource { DataSource= yourList};
    combo2.DataSource = new BindingSource { DataSource= yourList};
    

  • 也作为另一种选择:

    • 您可以使用不同的 BindingContext 用于您的组合框.这样,即使您将它们绑定到单个列表,它们也不再同步.

    • You can use different BindingContext for your combo boxes. This way even when you bind them to a single list, they are not sync anymore.

    combo1.BindingContext = new BindingContext();
    combo1.DataSource = yourList;
    combo2.BindingContext = new BindingContext();
    combo2.DataSource = yourList;
    

    事实上,表单的所有控件都使用一个共享的BindingContext.当您将 2 个控件绑定到同一个数据源时,它们也会以这种方式使用相同的 BindingManagerBase,例如,当您移动到下一条记录时,所有控件都会移动到下一条记录,显示来自绑定属性的值下一个记录.这与您从组合框中看到的行为相同.对使用相同 BindingManagerBase 的控件进行同步是一种理想的行为.无论如何,有时我们不需要这种行为.帖子分享了原因和解决方法.

    In fact all controls of the form use a shared BindingContext. When you bind 2 controls to a same data source, then they also use the same BindingManagerBase this way, when you for example move to next record, all controls move to next record an show value from bound property of next record. This is the same behavior that you are seeing from your combo boxes. Being sync for controls which are using the same BindingManagerBase is a desired behavior. Anyway sometimes we don't need such behavior. The post shares the reason and the solution.

    这篇关于将多个 ComboBox 绑定到单个列表 - 问题:当我选择一个项目时,所有组合框都会更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

    本文标题为:将多个 ComboBox 绑定到单个列表 - 问题:当我选择一个项目时,所有组合框都会更改

    基础教程推荐