【问题标题】:INotifyPropertyChanged with a List带有列表的 INotifyPropertyChanged
【发布时间】:2017-09-19 00:03:39
【问题描述】:

将 INotifyPropertyChanged 与列表一起使用的正确方法是什么?

我有一个 CertInfo 类,它是一个认证类:

    namespace ResumeApp
{
    public class CertInfo
    {
        public DateTime AcquiredDate { get; set; }
        public String Certification { get; set; }
        public bool Enabled { get; set; }
        public int UserId { get; set; }

        public CertInfo()
        {}

        public CertInfo(DateTime acquiredDate, String cert, bool enabled, int userId)
        {
            this.AcquiredDate = acquiredDate;
            this.Certification = cert;
            this.Enabled = enabled;
            this.UserId = userId;
        }
    }
}

我有一个 INotifyPropertyChanged 的​​ Resume 类。我不确定如何将通知与列表一起使用。这是我的简历课程:

    namespace ResumeApp
{
    public class Resume : INotifyPropertyChanged
    {
        private PersonalInfo personal;
        private int userId;
        private ObservableCollection<CertInfo> certList;

        public event PropertyChangedEventHandler PropertyChanged;

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

        public Resume()
        {
            personal = new PersonalInfo();
            userId = 0;
            certList = new ObservableCollection<CertInfo>();
        }

        public PersonalInfo Personal
        {
            get { return personal; }
            set
            {
                if (value != null)
                {
                    personal = value;
                    OnPropertyChanged("Personal");
                }
            }
        }

        public int UserId
        {
            get { return userId; }
            set
            {
                if (value != 0)
                {
                    userId = value;
                    OnPropertyChanged("UserId");
                }
            }
        }

        public ObservableCollection<CertInfo> CertList
        {
            get { return certList; }
            set
            {
                if(value != null)
                {
                    certList = value;
                    OnPropertyChanged("CertList");
                }
            }
        }
    }
}

对吗?

谢谢

【问题讨论】:

  • CertList 属性的值是对 ObservableCollection 实例的引用。每当该值更改时调用 PropertyChanged 并且与其他属性没有区别
  • 您知道如果 ResumeApp 实例的属性发生更改,您将不会收到通知吗?
  • 它将是一个 UWP 应用程序。我应该创建一个 ResumeController 并让通知在这个控制器中吗?然后,将我的 xaml DataContext 绑定到控制器?
  • 如果您需要通知(用于绑定)在 ResumeApp 上实施 INotifyPropertyChanged
  • 我现在就是这样。

标签: c# uwp observablecollection inotifypropertychanged


【解决方案1】:

如果您打算在 Resume 的实例中更改 ObservableCollection&lt;CertInfo&gt; 的实际实例,那将是正确的。

但是,在大多数情况下,使用 get-only 属性声明这种类型的关联就足够了,因为集合本身实际上永远不会改变,只有它的元素(添加/删除)。ObservableCollection&lt;T&gt; 本身实现了@987654321 @,通知 UI 这些修改。

您可以将 CertList-Property 剥离到此声明中:

public ObservableCollection<CertInfo> CertList { get; } = new ObservableCollection<CertInfo>();

要使用给定集合预初始化实例,我建议使用以下构造函数:

public Resume(ObservableCollection<CertInfo> certList) {
  //null-checks ommited
  CertList = certlist;
  //more initialization
  userId = 0;

}
public Resume(IEnumerable<CertInfo> certList) : this (new ObservableCollection(certlist) {

}
public Resume() : this(new ObservableCollection<CertInfo>()) {
}

【讨论】:

  • BTW ObservableCollection 也实现了 INotifyPropertyChanged
  • 谢谢。如何初始化 Resume,将 CertInfo 列表传递给我的 ObservableCollection CertList?
  • 只要定义一个合适的构造函数,传入集合。缩短的只读属性也可以在构造函数中设置。
  • 如果您需要在构造函数之外更改整个集合,只需触发 PropertyChanged-Event,就像您在初始示例中所做的那样。
  • 谢谢。 ObservableCollection certList 接受 List 类型?
猜你喜欢
  • 1970-01-01
  • 2015-06-22
  • 2014-03-15
  • 1970-01-01
  • 1970-01-01
  • 2017-07-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多