链收集的事件



我正在尝试查看如何将x observableCollections的数量链接在一起。CollectionChanged事件,作为N级深度对象树暴露于单个父母级别级别收集事件,该事件是消费者可以听吗?从本质上讲,我想将所有儿童收集的活动汇合到最高的父母。我注意到解决类似问题的许多解决方案可以假设固定数量的水平,例如2。我的想法是支持任何级别的深度。

最初,我希望我可以将fieldinfos的实例传递给子构造函数,然后直接附加到处理程序上。但是,我遇到了一个错误,说"事件'Collection Changed'只能出现在 =或 - =。

的左侧

谢谢,

public class FieldInfos
    {
        public event NotifyCollectionChangedEventHandler CollectionChanged;
        private ObservableCollection<Field> _fields;
        public ObservableCollection<Field> Fields => _fields ?? (_fields = new ObservableCollection<Field>());
    }

    public class Field
    {
        public string Name;
        private ObservableCollection<FieldInstance> _instances;
        public ObservableCollection<FieldInstance> Instances => _instances ?? (_instances = new ObservableCollection<FieldInstance>());
    }
    public class FieldInstance
    {
        public string Id { get; set; }
    }

最简单的方法是子类原始 ObservableCollection<T>

您至少需要一个接口来避免协方差问题。您还可以拥有自己的类来实现INotifyDescendantsChanged接口。

public interface INotifyDescendantsChanged
{
    event NotifyCollectionChangedEventHandler DescendantsChanged;
}
public class ObservableBubbleCollection<T> : ObservableCollection<T>, INotifyDescendantsChanged
{
    public event NotifyCollectionChangedEventHandler DescendantsChanged;
    protected virtual void OnDescendantsChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        NotifyCollectionChangedEventHandler handler = DescendantsChanged;
        if (handler != null)
            handler(sender, e);
    }
    private readonly Func<T, INotifyDescendantsChanged> childSelector;
    public ObservableBubbleCollection() { }
    public ObservableBubbleCollection(Func<T, INotifyDescendantsChanged> childSelector)
    {
        this.childSelector = childSelector;
    }
    protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
    {
        base.OnCollectionChanged(e);
        OnDescendantsChanged(this, e);
        if (childSelector == null)
            return;
        if (e.NewItems != null)
            foreach (var item in e.NewItems.Cast<T>())
                childSelector(item).DescendantsChanged += OnDescendantsChanged;
        if (e.OldItems != null)
            foreach (var item in e.OldItems.Cast<T>())
                childSelector(item).DescendantsChanged -= OnDescendantsChanged;
    }
}

要使用它,替换ObservableCollection的实例并将选择器传递给集合。

public class FieldInfos
{
    private ObservableBubbleCollection<Field> _fields;
    public ObservableBubbleCollection<Field> Fields => _fields ?? (_fields = new ObservableBubbleCollection<Field>(fi => fi.Instances));
}
public class Field
{
    public string Name;
    private ObservableBubbleCollection<FieldInstance> _instances;
    public ObservableBubbleCollection<FieldInstance> Instances => _instances ?? (_instances = new ObservableBubbleCollection<FieldInstance>());
}
public class FieldInstance
{
    public string Id { get; set; }
}
static class Program
{
    static void Main(string[] args)
    {
        var fi = new FieldInfos();
        fi.Fields.DescendantsChanged += (sender, e) =>
        {
            Console.WriteLine("Change from {0}", sender.GetType());
        };
        var field = new Field();
        fi.Fields.Add(field);
        field.Instances.Add(new FieldInstance());
        Console.ReadLine();
    }
}

相关内容

  • 没有找到相关文章

最新更新