2015-06-16 47 views
1

我有一个包含我的客户的详细信息的类。绑定的WinForms ListBox中收集到列表<object>

class CustomerData : INotifyPropertyChanged 
{ 
    private string _Name; 
    public string Name 
    { 
     get 
     { return _Name } 
     set 
     { 
      _Name = value; 
      OnPropertyChanged("Name"); 
     } 
    } 

    // Lots of other properties configured. 
} 

我也有CustomerDataList<CustomerData> MyData;

的名单我现在databinding个人CustomerData对象textboxes在正常工作下面的方法。

this.NameTxtBox.DataBindings.Add("Text", MyCustomer, "Name", false, DataSourceUpdateMode.OnPropertyChanged); 

我努力寻找到列表中的每个MyData对象绑定到一个ListBox的方式。

我想让MyData列表中的每个对象显示在显示名称的列表框中。

我已经尝试设置DataSource等于MyData列表和设置DisplayMember到“姓名”然而,当我将项目添加到MyData列表listbox不会更新。

有关如何完成的任何想法?

+2

你检查这一点:http://stackoverflow.com/questions/2675067/binding-listbox-to-listobject? –

+0

是的,我已经试过了。但是,当我添加项目到我的列表ListBox不更新。 – CathalMF

+2

winforms不使用观察系统。因此你必须将对象推送到列表框本身。 – JSJ

回答

1

我发现List<T>不允许在绑定列表被修改时更新ListBox。 为了得到这个工作,你需要使用BindingList<T>

BindingList<CustomerData> MyData = new BindingList<CustomerData>(); 

MyListBox.DataSource = MyData; 
MyListBox.DisplayMember = "Name"; 

MyData.Add(new CustomerData(){ Name = "Jimmy" }); //<-- This causes the ListBox to update with the new entry Jimmy. 
相关问题