一个关于 事件接口的问题
public class MVP :INotifyPropertyChanged
{
private int _ID;
private string _FullName;
private string _Speciality;
private decimal _AnnualIncome;
public MVP()
{
this._ID = -1;
this._FullName = String.Empty;
this._Speciality = String.Empty;
this._AnnualIncome = 0.0m;
}
public MVP(int ID, string FullName, string Speciality, decimal AnnualIncome)
{
_ID = ID;
_FullName = FullName;
_Speciality = Speciality;
_AnnualIncome = AnnualIncome;
}
//// 用来在清单方块中显示数据。
//public string DisplayData
//{
// get
// {
// return this.ID + ": " + this.FullName + " - " + this.Speciality + " - " + this.AnnualIncome.ToString("C");
// }
//}
public int ID
{
get
{
return _ID;
}
set
{
_ID = value;
OnPropertyChanged("ID");
}
}
public string FullName
{
get
{
return _FullName;
}
set
{
_FullName = value;
OnPropertyChanged("FullName");
}
}
public string Speciality
{
get
{
return _Speciality;
}
set
{
_Speciality = value;
OnPropertyChanged("Speciality");
}
}
public decimal AnnualIncome
{
get
{
return _AnnualIncome;
}
set
{
if (value!=_AnnualIncome)
{
OnPropertyChanged("AnnualIncome");
}
}
}
public override string ToString()
{
//字符串‘C’ 使得Decimal数值显示成一个货币值
return this.ID + ":" + this.FullName + " - " + this.Speciality + " - " + this.AnnualIncome.ToString("C");
}
// 在更改属性值时发生。
public event PropertyChangedEventHandler PropertyChanged;
// propertyName: 已更改的属性名。
protected void OnPropertyChanged(string propertyName)
{
if (PropertyChanged!=null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
以上代码是没有任何问题的。但是我有一个地方不明白:为什么这里不用进行事件订阅。按事件的定义来说,不是必须有一个形如“ this.button1.Click += new System.EventHandler(this.button1_Click);”的事件订阅吗?但是这个实现这个INotifyPropertyChanged接口,却可以直接激发事件?请问大神这是为什么? --------------------编程问答-------------------- 应该是在调用这个类的时候指定事件处理程序,和这个接口没有关系。
Mvp m=new Mvp(1, "FullName", "Speciality", 100.0);
m.PropertyChangedEventHandler+=new System.EventHandler(this.button1_Click);
这个时候修改m的属性的值,就会调用button1_Click。 --------------------编程问答-------------------- 怎么没有事件,这PropertyChanged是啥?如果PropertyChanged没有其他类访问来绑定,你能触发
PropertyChanged(this, new PropertyChangedEventArgs(propertyName))
成功才怪呢!!
补充:.NET技术 , C#