BindingList在何处/何时/如何显示 将 PropertyChanged 转换/连接到 ListChanged 事件

发布于 2024-07-29 10:25:43 字数 1527 浏览 8 评论 0原文

我有一个全部实现 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤独岁月 2024-08-05 10:25:43

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

I think the problem is that you're calling this.Items.Add() instead of this.Add(). The Items property returns the base List<T>, whose Add() method doesn't have the functionality you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文