我有一个对象层次结构,所有这些对象都实现了INotifyPropertyChanged。我还有一个从BindingList派生的自定义列表。
我的理解是,当我向列表中添加一个影响元素INotifyPropertyChanged的对象时,不知何故,PropertyChanged事件会自动连接到/转换为ListChanged事件。
但是,在我将列表设置为DataGridView的数据源后,当我更改网格中的值时,ListChanged事件不会触发...当我进入代码中时,发现PropertyChanged()事件没有触发,因为它是null,我假设这意味着它没有被连接起来/转换成绑定列表的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));
}
}
}下面是我的自定义list类。
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]));
}
}
}有什么想法或建议吗?
谢谢!
乔希
发布于 2009-07-20 19:20:18
我认为问题在于您调用的是this.Items.Add()而不是this.Add()。Items属性返回基本List<T>,它的Add()方法没有您想要的功能。
https://stackoverflow.com/questions/1155325
复制相似问题