无法将ComboBox SelectedItem用作级联ComboBox的BindingSource

时间:2012-03-27 20:06:34

标签: binding combobox selecteditem

我的表单上有2个ComboBox。我按如下方式创建绑定:

TestClass myclass = new TestClass("Instruments");

myclass.Add(instr1 = new TestClass("INSTR1"));
myclass.Add(instr2 = new TestClass("INSTR2"));
myclass.Add(instr3 = new TestClass("INSTR3"));
myclass.Add(instr4 = new TestClass("INSTR4"));

instr1.Add(app1 = new TestClass("app1"));
instr1.Add(app2 = new TestClass("app2"));
instr1.Add(app3 = new TestClass("app3"));
instr1.Add(app4 = new TestClass("app4"));

instr2.Add(app5 = new TestClass("app5"));
instr2.Add(app6 = new TestClass("app6"));
instr2.Add(app7 = new TestClass("app7"));
instr2.Add(app8 = new TestClass("app8"));

mysource = new BindingSource(myclass, null);
selectedComboBox1.DataSource = mysource;
selectedComboBox1.DisplayMember = "NAME";

mysource2 = new BindingSource(selectedComboBox1, "SelectedItem");
selectedComboBox2.DataSource = mysource2;
selectedComboBox2.DisplayMember = "NAME";

用于绑定的类如下所示

class TestClass : BindingList<TestClass>, INotifyPropertyChanged
{
    public event RunTestChanged RunTestChangedEventHandler;

    public TestClass()
    {
        this.test = "";
        this.name = "";
        this.runTest = true;
    }

    public TestClass(string name)
    {
        this.test = "";
        this.name = name;
        this.runTest = true;
    }

    public TestClass LIST
    {
        get
        {
            return this;
        }
    }

    public string NAME
    {
        get
        {
            return this.name;
        }
        set
        {
            this.name = value;
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs("NAME"));
            }
        }
    }

    public string TEST
    {
        get
        {
            return this.test;
        }
        set
        {
            this.test = value;
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs("TEST"));
            }
        }
    }


    public bool RUNTEST
    {
        get
        {
            return runTest;
        }
        set
        {
            runTest = value;
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs("RUNTEST"));
            }
            RunTestArgs myargs = new RunTestArgs(value);

            if (RunTestChangedEventHandler != null)
            {
                RunTestChangedEventHandler(this, myargs);
            }
        }
    }


    private bool runTest;
    private string name;
    private string test;

    public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
}

当表单首次加载时,2个组合框被填充,因为它们应与预期的项目一起填充。但是,如果我更改了selectedComboBox1中的项目,则selectedComboBox2中的项目不会更新。我知道我可以订阅selectedComboBox1 SelectedIndexChanged事件,然后在selectedComboBox2上重新绑定DataSource,一切都会按预期工作。

例如:

    void selectedComboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        mysource2.DataSource = selectedComboBox1.SelectedItem;
        mysource2.DataMember = null;
    }

另一种可行的方法是按如下方式执行数据绑定:

    mysource = new BindingSource(myclass, null);
    mysource2 = new BindingSource(mysource, "LIST");
    mysource3 = new BindingSource(mysource2, "LIST");

    selectedComboBox1.DataSource = mysource;
    selectedComboBox1.DisplayMember = "NAME";
    selectedComboBox2.DataSource = mysource2;
    selectedComboBox2.DisplayMember = "NAME";

但是我想知道是否有办法避免必须以不同的方式订阅事件或执行数据绑定,只需使用SelectedItem属性通过BindingSource更新第二个ComboBox。最后,我很想知道如何通过SelectedItem数据绑定来更新BindingSource,如果它不可能阻止它工作。

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,并通过将姓名绑定到SelectedValue的{​​{1}}并将combobox设置为“ NAME ”属性来解决问题

ValueMember