C#利用委托实现属性改变时触发事件的范例
public class MyClass { public event EventHandler<PropertyChagedEventArgs> MyPropertyChanging; public event EventHandler<PropertyChagedEventArgs> MyPropertyChanged; private int _myProperty; public int MyProperty { get { return _myProperty; } set { if (value != _myProperty) { PropertyChagedEventArgs e = new PropertyChagedEventArgs("MyProperty", _myProperty, value);//初始化 if (this.MyPropertyChanging != null) { this.MyPropertyChanging(this, e); if (e.Cancel) return; } _myProperty = (int)e.NewValue; if (this.MyPropertyChanged != null) { this.MyPropertyChanged(this, e); } } } } } /// <summary> /// 通用的类 /// </summary> public class PropertyChagedEventArgs : EventArgs { public PropertyChagedEventArgs(string propertyName,object oldValue,object newValue) { PropertyName = propertyName; OldValue = oldValue; NewValue = newValue; } public bool Cancel { get; set; } public string PropertyName { get; private set; } public object OldValue { get; private set; } public object NewValue { get; set; } }