将两个组合框绑定到相同的数据源,每个组合将具有单独的行为

本文关键字:组合 数据源 单独 两个 绑定 | 更新日期: 2023-09-27 18:19:03

现在当我在第一个组合框中选择项目时,第二个组合框模仿选择。我希望能够在每个组合框单独选择。谢谢。

 List<W6AdminUIs2.DictionaryObject> taskStatuses = W6AdminUIs2.GlobalFunctions.GetDictionaryItems("TaskStatus");
    // Init the binding source of the statuses.
    BindingSource bsFromStatuses = new BindingSource();
    bsFromStatuses.DataSource = taskStatuses;
    //Bind the "From" combo box to binding source.
    cBoxFrom.DataSource = bsFromStatuses.DataSource;
    cBoxFrom.DisplayMember = "Name";
    cBoxFrom.ValueMember = "Key";
    // Init the binding source of the statuses.
    BindingSource bsToStatuses = new BindingSource();
    bsToStatuses.DataSource = taskStatuses;
    //Bind the "From" combo box to binding source.
    cBoxTo.DataSource = bsToStatuses.DataSource;
    cBoxTo.DisplayMember = "Name";
    cBoxTo.ValueMember = "Key";

将两个组合框绑定到相同的数据源,每个组合将具有单独的行为

我不知道你用的是什么类型的字典,但我用的是普通的字典,这段代码不是这样的:

Dictionary<string,string> dict = new Dictionary<string, string>();
dict.Add("S1", "Sample1");
dict.Add("S2", "Sample2");
dict.Add("S3", "Sample3");
dict.Add("S4", "Sample4");
comboBox1.DataSource = new BindingSource(dict, null);
comboBox1.DisplayMember = "value";
comboBox1.ValueMember = "key";
comboBox2.DataSource = new BindingSource(dict, null);
comboBox2.DisplayMember = "value";
comboBox2.ValueMember = "key";