我有一个这样组织的数据结构:
包含List<Symbol>的List<Graphic> ,其中包含List<Alias>等。
我想能够在别名/符号/图形中发生任何变化时在Graphic类中运行一个函数。 我可以看到这样做的最好方法是在三个类中的每一个上实现IPropertyChanged 。 但是,是否可以级联这些,同时获得Graphic的参考,以确切地改变了什么?
注意:更改通常是Alias的属性,但删除/添加或重命名Symbol也是合理的。
I have a data structure organised as such:
A List<Graphic> containing a List<Symbol> which contains a List<Alias> amongst other things.
I want to be able to run a function within the Graphic class whenever anything changes within an alias/symbol/graphic. The best way that I can see to do this would be to implement IPropertyChanged on each of the three classes. However, is it possible to cascade these whilst getting a reference to the Graphic as to what exactly changed?
Note: The changes will generally be to the properties within an Alias but it is just as plausible that a Symbol could be removed/added or renamed.
最满意答案
您可以利用实现INotifyCollectionChanged和INotifyPropertyChanged类ObservableCollection<T>
基本上,您需要创建派生类并重写某些方法
public class Data { public ObservableCollection<String> InnerCollection { get; set; } } public class collection : ObservableCollection<Data> { protected override void InsertItem(int index, Data item) { item.InnerCollection.CollectionChanged += InnerCollection_CollectionChanged; base.InsertItem(index, item); } private void InnerCollection_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { //Actually it does not make any sense. You may need to construct something special. But firing an event it would be enough OnCollectionChanged(e); } protected override void RemoveItem(int index) { var date = base.Items[index]; date.InnerCollection.CollectionChanged -= InnerCollection_CollectionChanged; base.RemoveItem(index); } }使用这样的东西,您可以根据需要嵌套事件。
You can leverage class ObservableCollection<T> that implements INotifyCollectionChanged and INotifyPropertyChanged
Basically, you need to create a derived class and override some methods
public class Data { public ObservableCollection<String> InnerCollection { get; set; } } public class collection : ObservableCollection<Data> { protected override void InsertItem(int index, Data item) { item.InnerCollection.CollectionChanged += InnerCollection_CollectionChanged; base.InsertItem(index, item); } private void InnerCollection_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { //Actually it does not make any sense. You may need to construct something special. But firing an event it would be enough OnCollectionChanged(e); } protected override void RemoveItem(int index) { var date = base.Items[index]; date.InnerCollection.CollectionChanged -= InnerCollection_CollectionChanged; base.RemoveItem(index); } }Using something like this, you can nest your events as deep as you want.
更多推荐
发布评论