首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >wpf INotifyCollectionChanged冗余

wpf INotifyCollectionChanged冗余
EN

Stack Overflow用户
提问于 2016-02-06 12:51:51
回答 1查看 142关注 0票数 0

我编写了一个自定义控件,我有两个集合依赖项属性SourceA和SourceB。您将在下面的代码中看到,依赖项属性'PropertyChanged‘方法完全相同,似乎有很多多余的代码。

代码语言:javascript
复制
SourceB_PropertyChanged...
SourceA_PropertyChanged...

我觉得我做得不对。在维护已更改的集合通知的同时,是否有一种方法可以消除冗余?

代码

代码语言:javascript
复制
using System.Collections;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace NexusEditor
{
    /// <summary>
    /// The main class that implements the network/flow-chart control.
    /// </summary>
    public partial class NexusEditor : Control
    {
        #region Dependency Property/Event Definitions

        private static readonly DependencyPropertyKey ItemsPropertyKey =
            DependencyProperty.RegisterReadOnly("Items", typeof(ObservableCollection<object>), typeof(NexusEditor),
                new FrameworkPropertyMetadata());
        public static readonly DependencyProperty ItemsProperty = ItemsPropertyKey.DependencyProperty;

        public static readonly DependencyProperty SourceAProperty =
            DependencyProperty.Register("SourceA", typeof(IEnumerable), typeof(NexusEditor),
                new FrameworkPropertyMetadata(SourceA_PropertyChanged));

        public static readonly DependencyProperty SourceBProperty =
            DependencyProperty.Register("SourceB", typeof(IEnumerable), typeof(NexusEditor),
                new FrameworkPropertyMetadata(SourceB_PropertyChanged));

        #endregion


        #region Constructors

        public NexusEditor()
        {
            // Create a collection to contain nodes.
            this.Items = new ObservableCollection<object>();
        }

        static NexusEditor()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(NexusEditor), new FrameworkPropertyMetadata(typeof(NexusEditor)));
        }

        #endregion

        #region properties

        public IEnumerable SourceA
        {
            get { return (IEnumerable)GetValue(SourceAProperty); }
            set { SetValue(SourceAProperty, value); }
        }

        public IEnumerable SourceB
        {
            get { return (IEnumerable)GetValue(SourceBProperty); }
            set { SetValue(SourceBProperty, value); }
        }

        /// <summary>
        /// Collection of items
        /// </summary>
        public ObservableCollection<object> Items
        {
            get { return (ObservableCollection<object>)GetValue(ItemsProperty); }
            private set { SetValue(ItemsPropertyKey, value); }
        }

        #endregion

        #region privates

        private static void UpdateItems(NexusEditor editor)
        {
            editor.Items.Clear();

            var sourceB = editor.SourceB as IEnumerable;
            if (sourceB != null)
            {
                foreach (object obj in sourceB)
                {
                    editor.Items.Add(obj);
                }
            }

            var sourceA = editor.SourceA as IEnumerable;
            if (sourceA != null)
            {
                foreach (object obj in sourceA)
                {
                    editor.Items.Add(obj);
                }
            }
        }

        /// <summary>
        /// Event raised when a new collection has been assigned to the SourceB property.
        /// </summary>
        private static void SourceB_PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            NexusEditor c = (NexusEditor)d;

            if (e.OldValue != null)
            {
                var notifyCollectionChanged = e.OldValue as INotifyCollectionChanged;
                if (notifyCollectionChanged != null)
                {
                    notifyCollectionChanged.CollectionChanged -= new NotifyCollectionChangedEventHandler(c.SourceB_CollectionChanged);
                }
            }

            if (e.NewValue != null)
            {
                var notifyCollectionChanged = e.NewValue as INotifyCollectionChanged;
                if (notifyCollectionChanged != null)
                {
                    notifyCollectionChanged.CollectionChanged += new NotifyCollectionChangedEventHandler(c.SourceB_CollectionChanged);
                }
            }

            UpdateItems(c);
        }

        /// <summary>
        /// Event raised when a node has been added to or removed from the collection assigned to 'NodesSource'.
        /// </summary>
        private void SourceB_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            UpdateItems(this);
        }


        /// <summary>
        /// Event raised when a new collection has been assigned to the SourceB property.
        /// </summary>
        private static void SourceA_PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            NexusEditor c = (NexusEditor)d;

            if (e.OldValue != null)
            {
                var notifyCollectionChanged = e.OldValue as INotifyCollectionChanged;
                if (notifyCollectionChanged != null)
                {
                    notifyCollectionChanged.CollectionChanged -= new NotifyCollectionChangedEventHandler(c.SourceA_CollectionChanged);
                }
            }

            if (e.NewValue != null)
            {
                var notifyCollectionChanged = e.NewValue as INotifyCollectionChanged;
                if (notifyCollectionChanged != null)
                {
                    notifyCollectionChanged.CollectionChanged += new NotifyCollectionChangedEventHandler(c.SourceA_CollectionChanged);
                }
            }

            UpdateItems(c);
        }

        /// <summary>
        /// Event raised when a node has been added to or removed from the collection assigned to 'NodesSource'.
        /// </summary>
        private void SourceA_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            UpdateItems(this);
        }

        #endregion

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-02-06 16:14:49

例如,为什么不创建一个名为PropertyChangedSource_PropertyChanged处理程序方法,并将其用于两个依赖项属性?

如果他们是完全一样的,你就可以这么做。

代码语言:javascript
复制
public static readonly DependencyProperty SourceAProperty =
    DependencyProperty.Register("SourceA", typeof(IEnumerable), typeof(NexusEditor), 
    new FrameworkPropertyMetadata(Source_PropertyChanged));

public static readonly DependencyProperty SourceBProperty =
    DependencyProperty.Register("SourceB", typeof(IEnumerable), typeof(NexusEditor),
    new FrameworkPropertyMetadata(Source_PropertyChanged));

处理程序:

代码语言:javascript
复制
private static void Source_PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    NexusEditor c = (NexusEditor)d;

    if (e.OldValue != null)
    {
        var notifyCollectionChanged = e.OldValue as INotifyCollectionChanged;
        if (notifyCollectionChanged != null)
        {
            notifyCollectionChanged.CollectionChanged -= new NotifyCollectionChangedEventHandler(c.Source_CollectionChanged);
        }
    }

    if (e.NewValue != null)
    {
        var notifyCollectionChanged = e.NewValue as INotifyCollectionChanged;
        if (notifyCollectionChanged != null)
        {
            notifyCollectionChanged.CollectionChanged += new NotifyCollectionChangedEventHandler(c.Source_CollectionChanged);
        }
    }

    UpdateItems(c);
}

对于已更改的集合:

代码语言:javascript
复制
private void Source_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
    UpdateItems(this);
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35241222

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档