简体   繁体   English

在许多控件上实现INotifyPropertyChanged的优雅方式

[英]Elegant way to implement INotifyPropertyChanged across many controls

I'm building a WPF application and I'm slowly uncovering some of the joys and also the frustrations of using WPF. 我正在构建一个WPF应用程序,我正在慢慢发现一些乐趣以及使用WPF的挫败感。 My latest question involves updating the UI using INotifyPropertyChanged 我的最新问题涉及使用INotifyPropertyChanged更新UI

My app has stacked UserControls with each UserControl containing multiple controls, so overall there are hundreds of controls which update every second providing live data. 我的应用程序将UserControls与每个包含多个控件的UserControl堆叠在一起,因此整体上有数百个控件每秒更新一次,提供实时数据。 In order to update all controls I'm using something similar to below which does currently work as intended. 为了更新所有控件我正在使用类似于下面的东西,这些东西目前正如预期的那样工作。

namespace ProjectXAML
{
    public partial class ProjectX : UserControl, INotifyPropertyChanged
    {


#region Declare Getter/Setter with INotifyPropertyChanged groupx3

        private string m_group1Text1;
        public string group1Text1
        {
            get
            {
                return m_group1Text1;
            }
            set
            {
                m_group1Text1 = value;
                NotifyPropertyChanged("group1Text1");
            }
        }

        private string m_group1Text2;
        public string group1Text2
        {
            get
            {
                return m_group1Text2;
            }
            set
            {
                m_group1Text2 = value;
                NotifyPropertyChanged("group1Text2");
            }
        }

        private string m_group2Text1;
        public string group2Text1
        {
            get
            {
                return m_group2Text1;
            }
            set
            {
                m_group2Text1 = value;
                NotifyPropertyChanged("group2Text1");
            }
        }

        private string m_group2Text2;
        public string group2Text2
        {
            get
            {
                return m_group2Text2;
            }
            set
            {
                m_group2Text2 = value;
                NotifyPropertyChanged("group2Text2");
            }
        }

        private string m_group3Text1;
        public string group3Text1 
        { 
            get
            {
                return m_group3Text1;
            }
            set
            {
                m_group3Text1 = value;
                NotifyPropertyChanged("group3Text1");
            }
        }

        private string m_group3Text2;
        public string group3Text2
        {
            get
            {
                return m_group3Text2;
            }
            set
            {
                m_group3Text2 = value;
                NotifyPropertyChanged("group3Text2");
            }
        }

 #endregion

 #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        /// Notifies the property changed.
        private void NotifyPropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }
 #endregion

}
}

My questions are: 我的问题是:

  1. Is there a more elegant way to raise PropertyChanged events for lots of controls rather than lots of get/set code? 是否有更优雅的方法来为许多控件而不是大量的get / set代码引发PropertyChanged事件?

  2. Is there a way to raise 1 PropertyChanged event covering the whole UserControl containing multiple controls instead of a separate event for every control? 有没有办法引发一个PropertyChanged事件,覆盖整个UserControl包含多个控件而不是每个控件的单独事件? Is there a better method than what I'm attempting? 有没有比我正在尝试的方法更好的方法?

For point 1 if you are using VS 2012 you can do the below 对于第1点,如果您使用的是VS 2012,则可以执行以下操作

private void SetProperty<T>(ref T field, T value, [CallerMemberName] string name = "")
{
    if (!EqualityComparer<T>.Default.Equals(field, value))
    {
        field = value;
        var handler = PropertyChanged;
        if (handler != null)
        {
          handler(this, new PropertyChangedEventArgs(name));
        }
    }
}

and then you can use your set property method without having to hard code the name of the properties. 然后您可以使用您的set属性方法,而无需硬编码属性的名称。

Note the above code is an except of the below link http://danrigby.com/2012/03/01/inotifypropertychanged-the-net-4-5-way/ 请注意上面的代码是以下链接除了http://danrigby.com/2012/03/01/inotifypropertychanged-the-net-4-5-way/

Use the design pattern model view controler . 使用设计模式模型视图控制器 So the model will raise the changes for you. 所以模型会为你提出改变。 Together with MVVM the controls will see with its dependency objects the changes and view them automatically. MVVM一起,控件将使用其依赖项对象查看更改并自动查看它们。

In strict reference to this part of your question..."Is there a way to raise 1 PropertyChanged event covering the whole UserControl containing ". 严格参考你问题的这一部分......“有没有办法引发一个覆盖整个UserControl的PropertyChanged事件”。

Yes, you can raise a PropertyChanged notification which says all my properties on my object are updated. 是的,您可以引发一个PropertyChanged通知,告知我对象上的所有属性都已更新。

Use: 采用:

NotifyPropertyChanged(null);

then this informs the listener of INotifyPropertyChanged that all properties have changed on an object. 然后,这会通知INotifyPropertyChanged的监听器,即对象上的所有属性都已更改。

This isn't normally used...and can be abused....and cause inefficient updates eg if you were only changing a few properties and used that. 这通常不被使用......并且可能被滥用....并导致低效更新,例如,如果您只更改了一些属性并使用了它。

But you could argue the case for using it if you have lots of properties in your object, that you were always changing anyway at the same time...and you wanted to collapse lots of individual notifications into 1 that was raised after you had modified all properties. 但是如果你的对象中有很多属性,那么你可以争论使用它的情况,你总是同时改变...并且你想要将很多单独的通知合并到你修改后引发的1中所有财产。

Example use case (ie presumes you are updating all your groups in some way): 示例用例(即假设您以某种方式更新所有组):

void UpdateAllGroupTextProperties()
{
    group1Text1 = "groupA";
    group1Text2 = "groupA2";
    group2Text1 = "groupB";
    group2Text2 = "groupB2";
    group3Text1 = "groupC";
    group3Text2 = "groupC2";
    NotifyPropertyChanged(null);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM