Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 具有嵌套可观察子集合的树结构_C#_Wpf_Events_Tree_Observablecollection - Fatal编程技术网

C# 具有嵌套可观察子集合的树结构

C# 具有嵌套可观察子集合的树结构,c#,wpf,events,tree,observablecollection,C#,Wpf,Events,Tree,Observablecollection,我有一个类似下面的类 class ModelCollection : ObservableCollection<MyModel> { public ModelCollection () { this.CollectionChanged += (s, e) => { if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAc

我有一个类似下面的类

class ModelCollection : ObservableCollection<MyModel>
{
    public ModelCollection ()
    {
        this.CollectionChanged += (s, e) =>
        {
            if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
            {
                //do something
            }
            if(e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove)
            {
                //do something
            }
        };
    }
    //etc..
}
一般来说,我有一个集合,它的根MyModel有MyModel类型的子对象,这些子对象有MyModel类型的子对象,它可以达到20个嵌套级别,因此它基本上是一个树结构


正如您在ModelCollection中看到的,我有一个事件监听器,我的问题是,考虑到我有数千个MyModel项和每个项有多个嵌套的level,有这么多的事件监听器可以吗?如果没有更好的建议,您可以使用so match事件监听器,但您必须维护它。此外,它还可能导致内存泄漏

另一个选项是在所有模型对象上使用INotifyPropertyChanged,例如:

public class MyModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public int id { get {...} set { OnPropertyChanged("id"); } }
    public string Name { get {...} set { OnPropertyChanged("Name"); } }
    public MyModel Parent { get {...} set { OnPropertyChanged("Parent"); } }
    public ModelCollection Descendants { get {...} set { OnPropertyChanged("Descendants"); } }
    public ModelCollection Children { get {...} set { OnPropertyChanged("Children"); } }

    private void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = this.PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
然后,您可以使用层次化数据上下文和类似WPF的功能,这些功能可以利用INotifyPropertyChanged实现,而无需您的任何工作

另一个选项是继承DependencyObject并使用DependencyProperties而不是简单的属性,WPF可以以类似的方式使用这些属性。然而,我认为DependencyObject在非UI元素的情况下可能是一种过分的杀伤力,在这种情况下,INotifyPropertyChanged是首选


更新:我注意到您没有提到出于UI原因需要ObservableCollection,因此我对WPF的评论可能是多余的。无论如何,我认为使用INotifyPropertyChanged是管理这些事情的一个好方法,我认为这也是标准方法。

很抱歉,忘了提到我正在使用wpf,是的,这棵树会在UI和代码中不断编辑,我只是不确定是否有大量的事件侦听器,我已经在模型类中使用了INotifyPropertyChanged,但为了清晰起见,在问题中删除了它,只包含了更改的集合
public class MyModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public int id { get {...} set { OnPropertyChanged("id"); } }
    public string Name { get {...} set { OnPropertyChanged("Name"); } }
    public MyModel Parent { get {...} set { OnPropertyChanged("Parent"); } }
    public ModelCollection Descendants { get {...} set { OnPropertyChanged("Descendants"); } }
    public ModelCollection Children { get {...} set { OnPropertyChanged("Children"); } }

    private void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = this.PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}