【发布时间】:2013-01-02 12:01:27
【问题描述】:
我有模特班员工:
public class Employee
{
public string Id { get; set; }
public string Name { get; set; }
public string LastName { get; set; }
}
并查看模型类EmployeeViewModel,其中包含Employee Object
public class EmployeeViewModel : INotifyPropertyChanged
{
public EmployeeViewModel()
{
}
private Employee currentEmployee;
public Employee CurrentEmployee
{
get { return this.currentEmployee; }
set
{
this.currentEmployee = value;
this.NotifyPropertyChanged("CurrentEmployee");
}
}
//Some code .....
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
现在视图 (WPF) 将使用视图模型中的Employee 对象作为ItemSource 来显示员工数据
现在的问题是:我在视图上有更新按钮,当我更改视图中的 Employee 属性时(通过文本框)我想更新模型(所以之后我可以更新数据库),如何从视图中更新此模型。
【问题讨论】:
-
如果您使用绑定到 CurrentEmployee,数据应该通过绑定存在,考虑在模型中实现 INotifyPropertyChanged,您知道如何获取按钮的命令(又名 ICommand)吗?分享 xaml 以获得更好的答案
标签: wpf mvvm inotifypropertychanged itemsource