我正在关注有关WPF和数据绑定的PluralSight教程。我的代码在尝试实现INotifyPropertyChanged
时出错。
public class Employee : INotifyPropertyChanged
{
public string Name { get; set; }
public string Title { get; set; }
public static Employee getEmployee()
{
var emp = new Employee() { Name = "Tom", Title = "Developer" };
return emp;
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged( [CallerMemberName] string caller = "")
{
if ( PropertyChanged != null)
{
PropertyChanged (this,
new PropertyChangingEventArgs (caller));
}
}
}
错误发生在最后一条语句上(在if语句的末尾),说:
"无法从System.ComponentModel.PropertyChangingEventArgs转换 到System.ComponentModel.PropertyChangedEventArgs"
答案 0 :(得分:8)
错误并没有真正说明,它说它无法从PropertyChan ging EventArgs转换为PropertyChan ged EventArgs。所以使用正确的参数类名:
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string caller = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this,
new PropertyChangedEventArgs(caller));
}
}
答案 1 :(得分:4)
您使用的是错误的事件arg。当您应该使用PropertyChangingEventArgs
PropertyChangedEventArgs
private void OnPropertyChanged( [CallerMemberName] string caller = "")
{
if ( PropertyChanged != null)
{
PropertyChanged (this, new PropertyChangedEventArgs (caller));
}
}