【发布时间】: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