NotifiableData.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Runtime.Serialization;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Input;
  10. using Aitex.Core.UI.MVVM;
  11. namespace MECF.Framework.Common.CommonData
  12. {
  13. [Serializable]
  14. [DataContract]
  15. public class NotifiableItem : INotifyPropertyChanged
  16. {
  17. public event PropertyChangedEventHandler PropertyChanged;
  18. public void InvokePropertyChanged(string propertyName)
  19. {
  20. if (PropertyChanged != null)
  21. {
  22. PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  23. }
  24. }
  25. public void InvokePropertyChanged()
  26. {
  27. PropertyInfo[] ps = this.GetType().GetProperties();
  28. foreach (PropertyInfo p in ps)
  29. {
  30. InvokePropertyChanged(p.Name);
  31. if (p.PropertyType == typeof(ICommand))
  32. {
  33. DelegateCommand<string> cmd = p.GetValue(this, null) as DelegateCommand<string>;
  34. if (cmd != null)
  35. cmd.RaiseCanExecuteChanged();
  36. }
  37. }
  38. FieldInfo[] fi = this.GetType().GetFields();
  39. foreach (FieldInfo p in fi)
  40. {
  41. InvokePropertyChanged(p.Name);
  42. if (p.FieldType == typeof(ICommand))
  43. {
  44. DelegateCommand<string> cmd = p.GetValue(this) as DelegateCommand<string>;
  45. if (cmd != null)
  46. cmd.RaiseCanExecuteChanged();
  47. }
  48. }
  49. }
  50. }
  51. }