无法绑定到DataSource上的属性或列状态

时间:2014-03-20 10:19:14

标签: winforms binding

为什么这样做:

        public Form1()
    {
        InitializeComponent();
        exCheckedListBox1.DataSource = Profiles;
        this.exCheckedListBox1.DataBindings.Add(new System.Windows.Forms.Binding("Tag", this, "States", true));

    }

    CheckedBindingList Profiles = new CheckedBindingList();

    public int States
    {
        get
        {
            return Profiles.States;
        }
        set
        {
            Profiles.States = value;
        }
    }

}

public class CheckedBindingList : List<string>
{
    public int States { get; set; }
}

但更改绑定到

this.exCheckedListBox1.DataBindings.Add(new System.Windows.Forms.Binding("Tag", this.Profiles, "States", true));

抛出异常? 非常非常感谢。我尝试从继承表单List的自定义列表类绑定字段。

异常 - 无法绑定到DataSource上的属性或列状态。 参数名称:dataMember

1 个答案:

答案 0 :(得分:0)

似乎System.Windows.Forms.Binding参数dataSource不能是从List&lt;继承的类。 T&gt;

这解决了这个问题:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        Load += new EventHandler(Form1_Load);
    }

    CheckedBindingList Profiles = new CheckedBindingList();
    SomeClass some_class = new SomeClass();

    public int States
    {
        get
        {
            return Profiles.States;
        }
        set
        {
            Profiles.States = value;
        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        exCheckedListBox1.DataSource = Profiles;
        exCheckedListBox1.DataBindings.Add(new System.Windows.Forms.Binding("Tag", some_class, "States", true));
    }
}
public class CheckedBindingList : List<string>
{
    public int States { get; set; }
}
public class SomeClass
{
    public int States { get; set; }
}