级联IPropertyChanged(Cascading IPropertyChanged)

编程入门 行业动态 更新时间:2024-10-21 03:50:27
级联IPropertyChanged(Cascading IPropertyChanged)

我有一个这样组织的数据结构:

包含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.

更多推荐

本文发布于:2023-08-01 15:38:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1359164.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:级联   IPropertyChanged   Cascading

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!