【发布时间】:2010-11-12 10:09:25
【问题描述】:
我有一个全部实现 INotifyPropertyChanged 的对象层次结构。我还有一个从 BindingList 派生的自定义列表。
我的理解是,当我将一个包含 INotifyPropertyChanged 元素的对象添加到列表时,PropertyChanged 事件会以某种方式自动连接/转换为 ListChanged 事件。
但是,在我将列表设置为 DataGridView 的数据源后,当我更改网格中的值时,ListChanged 事件不会触发...当我进入代码时,事实证明 PropertyChanged()事件没有触发,因为它是空的,我认为这意味着它没有被连接/转换为 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]));
}
}
}
有什么想法或建议吗?
谢谢!
乔什
【问题讨论】:
标签: datagridview datasource inotifypropertychanged bindinglist