BindingList在何处/何时/如何显示 将 PropertyChanged 转换/连接到 ListChanged 事件
我有一个全部实现 INotifyPropertyChanged 的对象层次结构。 我还有一个派生自 BindingList 的自定义列表。
据我了解,当我将一个实现 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]));
}
}
}
任何想法或建议?
谢谢!
乔什
I have a hierarchy of objects that all implement INotifyPropertyChanged. I also have a custom list that derives from BindingList.
It is my understanding that when I add an object that impelements INotifyPropertyChanged to the list, that somehow the PropertyChanged event is automagically wired-up / converted to the ListChanged event.
However, after I set my list as the datasource of my DataGridView, when I change a value in the grid, the ListChanged event does not fire... When I step into the code, it turns out that the PropertyChanged() event is not triggering becaue it is null, which I assume to mean that it is not getting wired-up / converted to the BindingList's ListChanged event, like its supposed to...
For example:
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));
}
}
}
And here is my custom list class...
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]));
}
}
}
Any ideas or suggestions?
Thanks!
Josh
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为问题在于您正在调用
this.Items.Add()
而不是this.Add()
。Items
属性返回基本List
,其Add()
方法没有您想要的功能。I think the problem is that you're calling
this.Items.Add()
instead ofthis.Add()
. TheItems
property returns the baseList<T>
, whoseAdd()
method doesn't have the functionality you want.