如何检测BindingList 中项目属性的更改?
我有一个具有属性A和B的自定义类Foo。我想在数据绑定控件中显示它。
我创建了一个类Foos : BindingList
。
为了更新Foos类的一些内部属性,我需要在列表中的项目上通知属性更改(我可以处理插入,删除等)。 你会如何实现这个function?
我应该从支持它的框架中的某个对象inheritanceFoo吗? 我想我可以创建一些事件,通知我是否有变化,但这是应该做的方式吗? 或者框架中是否有一些模式,这对我有帮助?
Foo应该实现INotifyPropertyChanged
和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现在应该更新。
[编辑添加:]此外, BindingList
类公开了两个事件 ,这些事件在添加或修改集合时通知您:
上述就是C#学习教程:如何检测BindingList 中项目属性的更改?分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
public void DoSomething() { BindingList foos = getBindingList(); foos.ListChanged += HandleFooChanged; } void HandleFooChanged(object sender, ListChangedEventArgs e) { MessageBox.Show(e.ListChangedType.ToString()); }
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/989714.html