绑定多个组合框到一个列表 - 问题:当我选择一个项目,所有的组合框更改 [英] Bind multiple ComboBox to a single List - Issue: When I choose an item, all combo boxes change

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

问题描述

我动态创建一个组合框阵列和数据源所有组合框是包含一些整数一个整数列表。但是,当我改变一个值例如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.

推荐答案

由于你们都组合框绑定到同一数据源 - 一个列表 - 他们用的是单一的 BindingManagerBase

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

所以,当你从下拉框中的一个选择项目,当前的 位置 的共同绑定管理碱基变化。和所有组合框进入到他们的共享数据源中的位置

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的

  • 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};
    


  • 另外,作为另一种选择:

    Also as another option:


    • 您可以使用不同的 的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.

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

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆