如何检测更改项目性质的的BindingList< T>? [英] How can I detect changes to item properties in the BindingList<T>?

查看:142
本文介绍了如何检测更改项目性质的的BindingList< T>?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义类Foo有属性A和B.我想在数据绑定控件中显示它。

I have a custom class Foo with properties A and B. I want to display it in a databinding control.

我创建了一个类 FOOS:的BindingList<富>

为了更新FOOS类内部的一些特性,我需要的属性更改通知时(我可以处理插入,删除等)对列表中的项目。你将如何实现该功能?

In order to update some internal properties of the Foos class I need to be notified of property changes (I can handle insertions, removals etc.) on the items in the list. How would you implement that functionality ?

我要继承富之处在于,支持该框架的一些对象呢?我想我可以创建如有变动,会通知我的事件,但就是这样它应该做?或者有没有在框架中的一些模式,这将帮助我吗?

Should I inherit Foo from some object in the framework that supports that ? I think I could create events that notify me if changes, but is that the way it should be done ? Or is there some pattern in the framework, that would help me ?

推荐答案

美孚应实施<一​​个href="http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx">INotifyPropertyChanged和<一href="http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanging.aspx">INotifyPropertyChanging接口。

public void Foo : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }

    private int _someValue;
    public int SomeValue
    {
        get { return _someValue; }
        set { _someValue = value; NotifyPropertyChanged("SomeValue"); }
    }
}

本的BindingList应挂钩到事件处理程序自动的,并且您设置了类调用PropertyChanged事件处理程序的GUI现在应该更新whever。

The BindingList should hook onto your event handler automagically, and your GUI should now update whever you set your class invokes the PropertyChanged event handler.

此外,的BindingList类exposted 两个事件该通知您时收集已被添加或修改

Additionally, the BindingList class exposted two events which notify you when the collection has been added to or modified:

public void DoSomething()
{
    BindingList<Foo> foos = getBindingList();
    foos.ListChanged += HandleFooChanged;
}

void HandleFooChanged(object sender, ListChangedEventArgs e)
{
    MessageBox.Show(e.ListChangedType.ToString());
}

这篇关于如何检测更改项目性质的的BindingList&LT; T&GT;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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