[C#] C#利用委托实现属性改变时触发事件的范例 →→→→→进入此内容的聊天室

来自 , 2019-04-30, 写在 C#, 查看 115 次.
URL http://www.code666.cn/view/1731592a
  1. public class MyClass
  2.     {
  3.         public event EventHandler<PropertyChagedEventArgs> MyPropertyChanging;
  4.         public event EventHandler<PropertyChagedEventArgs> MyPropertyChanged;
  5.  
  6.         private int _myProperty;
  7.         public int MyProperty
  8.         {
  9.             get { return _myProperty; }
  10.             set
  11.             {
  12.                 if (value != _myProperty)
  13.                 {
  14.                     PropertyChagedEventArgs e = new PropertyChagedEventArgs("MyProperty", _myProperty, value);//初始化
  15.                     if (this.MyPropertyChanging != null)
  16.                     {
  17.                         this.MyPropertyChanging(this, e);
  18.                         if (e.Cancel) return;
  19.                     }
  20.                     _myProperty = (int)e.NewValue;
  21.                     if (this.MyPropertyChanged != null)
  22.                     {
  23.                         this.MyPropertyChanged(this, e);
  24.                     }
  25.                 }
  26.             }
  27.         }
  28.  
  29.  
  30.     }
  31.  
  32.     /// <summary>
  33.     /// 通用的类
  34.     /// </summary>
  35.     public class PropertyChagedEventArgs : EventArgs
  36.     {
  37.         public PropertyChagedEventArgs(string propertyName,object oldValue,object newValue)
  38.         {
  39.             PropertyName = propertyName;
  40.             OldValue = oldValue;
  41.             NewValue = newValue;
  42.         }
  43.  
  44.         public bool Cancel { get; set; }
  45.         public string PropertyName { get; private set; }
  46.         public object OldValue { get; private set; }
  47.         public object NewValue { get; set; }
  48.     }
  49. //csharp/5975

回复 "C#利用委托实现属性改变时触发事件的范例"

这儿你可以回复上面这条便签

captcha