- 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; }
- }
- //该片段来自于http://www.codesnippet.cn/detail/250620134270.html
来源: http://www.codesnippet.cn/detail/250620134270.html