将Winforms ListBox集合绑定到List <object>

时间:2015-06-16 10:13:17

标签: c# winforms data-binding listbox

我有一个包含客户详细信息的课程。

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

   // Lots of other properties configured.
}

我还有一个CustomerData值列表List<CustomerData> MyData;

我目前databindingCustomerData个人textboxes对象this.NameTxtBox.DataBindings.Add("Text", MyCustomer, "Name", false, DataSourceUpdateMode.OnPropertyChanged); 以下方式工作正常。

MyData

我很难找到一种方法将列表ListBox中的每个对象绑定到DataSource

我希望在ListBox中显示MyData列表中的每个对象,显示名称。

我尝试将MyData设置为等于DisplayMember列表,并将MyData设置为&#34;名称&#34;但是,当我向listbox列表添加项目时,RewriteRule ^(en|ar)/properties/$ search.php?lang=$1 不会更新。

关于如何做到的任何想法?

1 个答案:

答案 0 :(得分:2)

我发现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.
相关问题