c# - 将 Winforms ListBox 集合绑定(bind)到 List<object>

标签 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;

我目前databinding一个人CustomerData反对 textboxes以下面的方式工作正常。

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

我正在努力寻找一种方法来绑定(bind)列表中的每个对象 MyDataListBox .

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

我试过设置 DataSource等于 MyData列出并设置 DisplayMember到“名称”但是当我将项目添加到MyData列出 listbox不更新。

关于如何做到这一点有什么想法吗?

最佳答案

我发现 List<T>修改绑定(bind)列表时将不允许更新 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.

关于c# - 将 Winforms ListBox 集合绑定(bind)到 List<object>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30864661/

相关文章:

C# DataGridView 右键单击​​到 ContextMenu 单击检索单元格值

c# - NetworkStream 连接自动关闭

c# - 如何在 c# (mono) 中导入 c 库 (.dll/.so)

wpf - 绑定(bind)组上的绑定(bind)表达式列表为空的解决方法

c# - HashAlgorithm.ComputeHash() 是有状态的吗?

window 尺寸

c# - ListView 项目在 winform 中选择

c# - 同时用户在数据绑定(bind) DataGridView 中进行更改时出现问题

wpf - 将 ListViewItem ContextMenu MenuItem 命令绑定(bind)到 ListView 的 ItemsSource 的 ViewModel

java - android数据绑定(bind)错误: cannot find symbol