我有一个WinForm应用程序,其网格包含每行的ComboBox.所有都绑定到同一个集合(该集合可能会改变,这就是为什么我不希望每个Combo都有不同的集合,也是内存成本).问题是,当我在一个组合中选择一个对象时,它会在每个组合中更改所选对象.这是一个可以运行并轻松复制的代码.
public Form1()
{
InitializeComponent();
this.comboBox1 = new System.Windows.Forms.ComboBox();
List<int> numList = new List<int>(){1,2,3,4};
this.comboBox1.FormattingEnabled = true;
this.comboBox1.Location = new System.Drawing.Point(33, 169);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(126, 21);
this.comboBox1.TabIndex = 3;
this.comboBox1.DataSource = numList; // BINDING TO NUMLIST
this.comboBox2 = new System.Windows.Forms.ComboBox();
this.comboBox2.FormattingEnabled = true;
this.comboBox2.Location = new System.Drawing.Point(243, 367);
this.comboBox2.Name = "comboBox2";
this.comboBox2.Size = new System.Drawing.Size(126, 21);
this.comboBox2.TabIndex = 4;
this.comboBox2.DataSource = numList; // BINDING TO NUMLIST ( THE SAME LIST
this.Controls.Add(this.comboBox2);
this.Controls.Add(this.comboBox1);
}
Run Code Online (Sandbox Code Playgroud)
只需创建一个表单并粘贴ComboBox 1和2的声明.任何想法如何发生这种情况.我的意思是如果它是一个简单的列表,它不会跟踪所选对象.DataSource背后发生了什么?