Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将Winforms列表框集合绑定到列表<;对象>;_C#_Winforms_Data Binding_Listbox - Fatal编程技术网

C# 将Winforms列表框集合绑定到列表<;对象>;

C# 将Winforms列表框集合绑定到列表<;对象>;,c#,winforms,data-binding,listbox,C#,Winforms,Data Binding,Listbox,我有一个包含客户详细信息的类 class CustomerData : INotifyPropertyChanged { private string _Name; public string Name { get { return _Name } set { _Name = value; OnPropertyChanged("Name");

我有一个包含客户详细信息的类

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

   // Lots of other properties configured.
}
我还有一个
CustomerData
值的列表
list MyData

我目前用下面的方法将个人
CustomerData
对象绑定到
textboxs
,效果很好

this.NameTxtBox.DataBindings.Add("Text", MyCustomer, "Name", false, DataSourceUpdateMode.OnPropertyChanged);
我正在努力找到一种方法,将列表
MyData
中的每个对象绑定到
ListBox

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

我已尝试将
数据源
设置为
MyData
列表,并将
DisplayMember
设置为“Name”,但当我向
MyData
列表添加项目时,
列表框
不会更新

有什么想法吗?

我发现当绑定列表被修改时,
List
将不允许更新列表框。 要使其正常工作,您需要使用
BindingList

BindingList MyData=newbindingList();
MyListBox.DataSource=MyData;
MyListBox.DisplayMember=“Name”;

添加(新CustomerData(){Name=“Jimmy”})//你检查过这个吗?是的,我试过了。但是,当我向列表中添加项目时,列表框不会更新。winforms不使用观察系统。所以你必须把对象推到列表框本身。啊,找到了。我需要使用BindingList而不是List。这允许列表框随着集合的修改而更新。
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.