视窗的奇怪行为窗体组合框控件 [英] Strange behavior of Windows Forms combobox control

查看:112
本文介绍了视窗的奇怪行为窗体组合框控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个小的桌面应用程序,并且有几个下拉列表(组合框-ES)的窗体上。我填充串,这将被用作所有这些数据源的列表。这是从我的Form.cs类的例子:

I am developing a small desktop app, and there are several drop-down lists (combobox-es) on my form. I populate a list of strings, which will be used as data source for all of them. Here is example from my Form.cs class:

List<string> datasource = new List<string>();
datasource.Add("string 1");
datasource.Add("string 2");



然后我把这个列表作为数据源到几个组合框:

Then I set this list as a data source to several comboboxes:

 cmbDataType1.DataSource = datasource;
 cmbDataType2.DataSource = datasource;

这一切都发生在同一个方法,它是从表单构造方法调用。
这里是奇怪的部分:我在其中的一个改变的选定值之后,同样的值将在另一设置。有没有 SelectedIndexChange 设置事件。我在什么地方搞砸了,但我不能把我的手指在那里...

This all happens in same method, which is called from the Form constructor. Here is the strange part: after I change a selected value in one of them, the same value will be set in the other one. There are no SelectedIndexChange events set. I have messed up somewhere, but I cant put my finger where...

推荐答案

这是你看到的是由设计行为。当您绑定同一个对象作为多个控件的数据源,所有控件共享相同的绑定源。

The behavior that you see is by design. When you bind the same object as the data source for multiple controls, all the controls share the same binding source.

如果您明确一个新的绑定源分配给每个控制,即使使用相同的数据源,所有的控件将被解放,将起到相互独立的:

If you explicitly assign a new binding source to each control, even while using the same data source, all controls will be unbound and will act independent of each other:

cmbDataType1.DataSource = new BindingSource(datasource, "");
cmbDataType2.DataSource = new BindingSource(datasource, "");

这篇关于视窗的奇怪行为窗体组合框控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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