我希望能够将列表绑定到列表框数据源,并且在修改列表时,列表框的 UI 会自动更新。(Winforms 不是 ASP)。这是一个示例:
private List<Foo> fooList = new List<Foo>();
private void Form1_Load(object sender, EventArgs e)
{
//Add first Foo in fooList
Foo foo1 = new Foo("bar1");
fooList.Add(foo1);
//Bind fooList to the listBox
listBox1.DataSource = fooList;
//I can see bar1 in the listbox as expected
}
private void button1_Click(object sender, EventArgs e)
{
//Add anthoter Foo in fooList
Foo foo2 = new Foo("bar2");
fooList.Add(foo2);
//I expect the listBox UI to be updated thanks to INotifyPropertyChanged, but it's not
}
class Foo : INotifyPropertyChanged
{
private string bar_ ;
public string Bar
{
get { return bar_; }
set
{
bar_ = value;
NotifyPropertyChanged("Bar");
}
}
public Foo(string bar)
{
this.Bar = bar;
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
public override string ToString()
{
return bar_;
}
}
如果我更换List<Foo> fooList = new List<Foo>();
,BindingList<Foo> fooList = new BindingList<Foo>();
那么它的工作原理。但我不想改变原来的傻瓜类型。我想要这样的工作:listBox1.DataSource = new BindingList<Foo>(fooList);
编辑:另外,我刚刚在这里阅读了Ilia Jerebtsov 的List<T> vs BindingList<T> 的优点/缺点:“当您将 BindingSource 的 DataSource 设置为 List<> 时,它会在内部创建一个 BindingList 来包装您的列表”。我认为我的示例只是表明这是不正确的:我的 List<> 似乎没有在内部包装到 BindingList<> 中。