[英]Fire an event when Collection Changed (add or remove)
I have a class which contains a list : 我有一个包含列表的类:
public class a
{
private List<MyType> _Children;
public Children
{
get {return(_Children);}
set {_Children = value ;}
}
}
I want to create an event and fire that event whenever my list (_Children here) is changed for example an item is added to it or removed from it or it's cleared . 我希望创建一个事件并在我的列表(此处为_Children)更改时触发该事件,例如,项目已添加到其中或从中删除或已清除。
thanks 谢谢
Change your list to an ObservableCollection<T>
. 将列表更改为
ObservableCollection<T>
。 It implements INotifyCollectionChanged , so you can subscribe to change events on it. 它实现了INotifyCollectionChanged ,因此您可以订阅更改它的事件。
Another option is to use BindingList<T>
, if you need full list semantics. 如果需要完整列表语义,另一个选择是使用
BindingList<T>
。
Or if you want to control the Add and remove methods and raise event, check Collection<T>
out. 或者,如果要控制“添加和删除方法”并引发事件,请选中“
Collection<T>
。
http://msdn.microsoft.com/en-us/library/ms132397.aspx http://msdn.microsoft.com/en-us/library/ms132397.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.