首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >BindingList<T>在何处/何时/如何将PropertyChanged转换/连接到ListChanged事件

BindingList<T>在何处/何时/如何将PropertyChanged转换/连接到ListChanged事件
EN

Stack Overflow用户
提问于 2009-07-20 19:08:28
回答 1查看 1.8K关注 0票数 3

我有一个对象层次结构,所有这些对象都实现了INotifyPropertyChanged。我还有一个从BindingList派生的自定义列表。

我的理解是,当我向列表中添加一个影响元素INotifyPropertyChanged的对象时,不知何故,PropertyChanged事件会自动连接到/转换为ListChanged事件。

但是,在我将列表设置为DataGridView的数据源后,当我更改网格中的值时,ListChanged事件不会触发...当我进入代码中时,发现PropertyChanged()事件没有触发,因为它是null,我假设这意味着它没有被连接起来/转换成绑定列表的ListChanged事件,就像它应该的那样……

例如:

代码语言:javascript
复制
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类。

代码语言:javascript
复制
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]));
          }
     }
}

有什么想法或建议吗?

谢谢!

乔希

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2009-07-20 19:20:18

我认为问题在于您调用的是this.Items.Add()而不是this.Add()Items属性返回基本List<T>,它的Add()方法没有您想要的功能。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1155325

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档