创建一个像BindingList这样的自定义集合,它与ListBox一起创建一个ListChanging事件 [英] Create a custom collection like BindingList that works with ListBox to create a ListChanging event

查看:209
本文介绍了创建一个像BindingList这样的自定义集合,它与ListBox一起创建一个ListChanging事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个情况,我需要知道什么时候项目是在集合中添加/删除/修改。

I have a situation where I need to know when an item is going to be added/removed/modified in the collection.

我尝试通过在一个类中继承BindingList来触发这些事件,但是添加事件不起作用。我发现它工作的唯一方法是覆盖EndNew()方法,但是我没有找到一种方法来获取在该方法中添加哪个对象(如果有人有一个解决方案,也可以)

I tried by inheriting BindingList in a class that will trigger these events, however the "adding" event doesn't work. The only way I found it working is by overriding EndNew() method, however I don't find a way to get which object is going to be added in this method (if someone has a solution for this, it's ok too!).

所以构建了一个全新的类,它继承自BindingList的相同接口/类,并实现了一切(我没有继承,但是,ICancelAddNew )

So built a totally new class which inherits from same interfaces/class of BindingList and implemented everything (I didn't inherit, however, ICancelAddNew).

我通过数据绑定绑定到我的列表框,我发现没有任何工作(列表事件既不会更改事件)。如何模拟列表框中的BindingList行为?

I bound it through databindings to my listbox and I find out that nothing works (listchanged events neither listchanging events). How can I simulate BindingList behavior on a listbox?

任何建议都非常感谢,我没有任何其他解决方法的想法

Any suggestion heavily appreciated, I don't have any other ideas for a workaround

编辑1:

这是我的集合: http://pastie.org/1978601
这是我将集合绑定到列表框的方式

This is my collection: http://pastie.org/1978601 And this is how I bind the collection to the ListBox

        SpellCasterManager.CurrentProfile.ButtonsMacro.ListChanged += new ListChangedEventHandler(ButtonsMacro_ListChanged);
        SpellCasterManager.CurrentProfile.ButtonsMacro.ListChanging += new Expand.ComponentModel.ListChangingEventHandler(ButtonsMacro_ListChanging);
gumpButton.DataBindings.Add("Value", SpellCasterManager.CurrentProfile.ButtonsMacro, "GumpIndex", false, DataSourceUpdateMode.OnPropertyChanged);

实际上在订阅的事件下只有一个MessageBox.Show(bla);

Actually under subscribed events there is just a MessageBox.Show("bla");

推荐答案


  • 您的集合将不会检测到现有项目中的属性更改,因为它不会挂接该项目的属性更改事件,因为它被添加到集合。

    • Your collection won't detect property changes in an existing item because it doesn't hook into the item's property changed events as it is added to the collection.

      BindingList< T> 在您的项目上收听PropertyChanged,并在一个ListChanged事件item被添加到BindingList中,它包括添加的索引。尝试在没有WinForms的测试应用程序。

      BindingList<T> does listen to PropertyChanged on your item and does fire a ListChanged event when an item is added to the BindingList and it does include the index at which is is added. Try it in a test app without WinForms.

      添加现有项目与AddNew()不同。 AddNew事件只在调用AddNew()时调用,并允许您提供新的实例。

      Adding an existing item is not the same as AddNew(). The AddingNew event is only called when AddNew() is called and allows you to supply the new instance.

      当涉及到WinForms时,事情变得更加复杂。有CurrencyManager要考虑,还有BindingSource。如果没有任何事件触发,请检查您是否使用您认为您的CurrencyManager / BindingSource。

      When WinForms is involved, things get more complicated. There is the CurrencyManager to think about and also BindingSource. If no events are firing at all then check to see if you are using the CurrencyManager/BindingSource you think you are.

      我不认为框架使用INotifyPropertyChanging,只有原始的INotifyPropertyChanged。您可能希望在BindingList上使用Reflector来查看挂钩是如何完成的,然后尝试将INotifyPropertyChanging加入到项目中。

      I don't think anything in the framework uses INotifyPropertyChanging, only the original INotifyPropertyChanged. You might want to use Reflector on BindingList to see how the hooking is done and then try to incorporate INotifyPropertyChanging if your item supports it.

      这篇关于创建一个像BindingList这样的自定义集合,它与ListBox一起创建一个ListChanging事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆