我有一组对象,它们都实现了INotifyPropertyChanged接口。我还有一个自定义列表,继承自BindingList。
据我所知,当我将实现INotifyPropertyChanged的对象添加到列表中时,某种方式会自动将PropertyChanged事件连接转换为ListChanged事件。
然而,当我将我的列表设置为DataGridView的数据源后,在网格中更改值时,ListChanged事件不会触发…… 当我进入代码时,结果发现PropertyChanged()事件没有触发,因为它是null,我认为这意味着它没有像应该那样连接转换为BindingList的ListChanged事件……
例如:
任何想法或建议吗?
谢谢!
Josh
据我所知,当我将实现INotifyPropertyChanged的对象添加到列表中时,某种方式会自动将PropertyChanged事件连接转换为ListChanged事件。
然而,当我将我的列表设置为DataGridView的数据源后,在网格中更改值时,ListChanged事件不会触发…… 当我进入代码时,结果发现PropertyChanged()事件没有触发,因为它是null,我认为这意味着它没有像应该那样连接转换为BindingList的ListChanged事件……
例如:
public class Foo : INotifyPropertyChanged
{
//Properties...
private string _bar = string.Empty;
public string Bar
{
get { return this._bar; }
set
{
if (this._bar != value)
{
this._bar = value;
this.NotifyPropertyChanged("Bar");
}
}
}
//Constructor(s)...
public Foo(object seed)
{
this._bar = (string)object;
}
//PropertyChanged event handling...
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(String info)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
这是我的自定义列表类...
public class FooBarList : BindingList<Foo>
{
public FooBarList(object[] seed)
{
for (int i = 0; i < seed.Length; i++)
{
this.Items.Add(new Foo(this._seed[i]));
}
}
}
任何想法或建议吗?
谢谢!
Josh