リストをリストボックス データソースにバインドできるようにしたいのですが、リストが変更されると、リストボックスの UI が自動的に更新されます。(ASPではなくWinform)。ここにサンプルがあります:
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> Advantages/DisAdvantagesを読みました:「BindingSource の DataSource を List<> に設定すると、リストをラップする BindingList が内部的に作成されます」。私のサンプルは、これが正しくないことを示しているだけだと思います。私の List<> は、内部で BindingList<> にラップされていないようです。