我有一个正在研究的wpf MVVM项目,我正在使用棱镜,所以INotifyPropertyChanged = SetProperty。我的模型是ComplaintModel:
public class ComplaintModel:BindableBase, IDataErrorInfo
{
private Person _complaintPerson;
private List<EmployeeModel> _crewList;
public string MyStreet { get; set; }
public string Complaint { get; set; }
public string Response { get; set; }
public DateTime? DateOfResponse { get; set; }
public DateTime EntryDate { get; set; }
public bool? Callout { get; set; }
public string Quad { get; set; }
public string Tap { get; set; }
public DateTime? Modified { get; set; }
public Person ComplaintPerson
{
get { return _complaintPerson; }
set { SetProperty(ref (_complaintPerson), value); }
}
public List<EmployeeModel> CrewList
{
get { return _crewList; }
set { SetProperty(ref (_crewList), value); }
}
public int ComplaintID { get; set; }
和ViewModel中的属性:
private ComplaintModel _selectedComplaint;
public ComplaintModel SelectedComplaint
{
get { return _selectedComplaint; }
set { SetProperty(ref (_selectedComplaint), value); }
}
EmployeeModel是
public class EmployeeModel:BindableBase,IDataErrorInfo
{
private string _phone;
private string _firstName;
private string _lastName;
public int Id { get; set; }
public string FirstName
{
get { return _firstName; }
set { SetProperty(ref(_firstName),value); }
}
public string LastName
{
get { return _lastName; }
set { SetProperty(ref(_lastName),value); }
}
public string Phone
{
get { return _phone; }
set { SetProperty(ref (_phone), value); }
}
所以我尝试添加员工:
private void AddEmployee()
{
if (SelectedEmployee == null)
{
SetMessage(Messages.ChooseEmployeeMessage);
return;
}
if (IsReadOnly)
{
SetMessage(Messages.MakeEditableMessage);
return;
}
if (SelectedComplaint.CrewList == null)
{
SelectedComplaint.CrewList = new List<EmployeeModel>();
}
var myList = SelectedComplaint.CrewList;
var employee = new EmployeeModel() {FirstName = "James", Id = 1, LastName = "Tays", Phone = "1234567"};
myList.Add(employee);
employee = new EmployeeModel() { FirstName = "John", Id = 2, LastName = "Doe", Phone = "1234567" };
myList.Add(employee);
SelectedComplaint.CrewList = myList;
SelectedEmployee = null;
}
这会将两名员工添加到CrewList,但不会更新视图。有趣的是,当我将var myList = SelectedComplaint.CrewList
更改为var myList = new List<EmployeeModel>();
时,它将添加2名员工并更新视图。
我也尝试了SelectedComplaint.CrewList.Add(employee)
,但这并没有更新视图。
答案 0 :(得分:1)
尝试将CrewList类型声明为&#34; observablecollection&#34;而不是&#34; List&#34;它会在您添加每个员工时更新视图。
ObservableCollection<EmployeeModel> CrewList
答案 1 :(得分:1)
调用Add不会被视为更改属性。这就是为什么: 当你这样做时:
var myList = SelectedComplaint.CrewList;
var employee = new EmployeeModel() {FirstName = "James", Id = 1, LastName = "Tays", Phone = "1234567"};
myList.Add(employee);
employee = new EmployeeModel() { FirstName = "John", Id = 2, LastName = "Doe", Phone = "1234567" };
myList.Add(employee);
SelectedComplaint.CrewList = myList;
您:
1)调用CrewList
的getter。这将返回参考_crewList
对象
2)您Add
Employee
- 这会修改列表,但这并不会调用CrewList
的设置者 - 因此,视图不会被通知修改列表
3)将CrewList
属性设置为myList变量的值。但是,请注意我在第1点中所说的内容 - myList
是对同一个对象的引用 - 这不会改变你的属性(如果在setter中没有改变对象,可能Prism没有通知视图)
我的建议是将List
更改为ObservableCollection
- 此课程还有一个&#34;事件&#34;名为NotifyCollectionChanged
- 当您致电ObservableCollection
,Add()
等时,会由Remove()
自动提出。这会为您通知视图。
答案 2 :(得分:0)
您需要更改
public List<EmployeeModel> CrewList
{
get { return _crewList; }
set { SetProperty(ref (_crewList), value); }
}
到
public ObservableCollection<EmployeeModel> CrewList {get;set}
这将自动调用集合上的NotifyCollectionChange事件。
This帖子包含有关列表和可观察集合之间差异的更多信息。