绑定ListBox seletedltems

时间:2014-10-29 06:21:55

标签: c# winforms entity-framework-4

尝试绑定ListBox c BindingSource。

有这样一个实体

// Entitys
public class Sp
{
    public int Id { get; set; }
    public int? IdTemp { get; set; } //Foreing Key 
    public Temp Temp { get; set; }
    public string Name { get; set; }
} 
public class Temp
{
    public int Id { get; set; }
    public string Caption { get; set; }
    public ICollection<Sp> Sps { get; set; } 
}

创建BindingSource

var b = new BindingSource{ DataSource = new BindingList<Temp>().ToList() };

创建绑定

listBox1.SelectionMode = SelectionMode.MultiSimple;
//Throws an error: 'SelectedItems' because it is read-only
listBox1.DataBindings.Add("SelectedItems", B, "Sps.IdTemp", true);

1 个答案:

答案 0 :(得分:0)

检查此答案,了解如何正确使用DataSourceDataBindingsHow to add new item into listbox that using datasource?

将任何内容绑定到SelectedItems的{​​{1}}属性是没有意义的。 相反,您可以绑定从ListBoxDataSource属性或类似内容的内容。