只要Model实现这个接口就可以了。我写了个demo
view层:(datacontext绑定到vm层)
Model层:Person类实现INotifyPropertyChanged接口,有一个PName属性
private string pname;
public string PName
{
get { return pname; }
set
{
if (PropertyChanged != null)
{
pname = value;
PropertyChanged(this, new PropertyChangedEventArgs("PName"));
}
}
}
ViewModel层:
public class PersonViewModel
{
private Person person1;
public Person Person1
{
get { return person1; }
set
{
person1 = value;
}
}
public void UpdateName()
{
if (person1 != null)
{
person1.PName = "aaa";
}
}
public ICommand UpdateNameCommand { get; set; }
public PersonViewModel()
{
person1 = new Person();
UpdateNameCommand = new MyCommand(this);
}
public event PropertyChangedEventHandler PropertyChanged;
}
命令:
public class MyCommand : ICommand
{
public PersonViewModel vm = null;
public MyCommand(PersonViewModel vm)
{
this.vm = vm;
}
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged
{
add { }
remove { }
}
public void Execute(object parameter)
{
vm.UpdateName();
}
}
一般情况 只有 ViewModel 实现 INotifyPropertyChanged!
但你这个 需求 Model 也要实现!