ViewModelBase.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.ComponentModel;
  6. using System.Windows.Threading;
  7. using System.Linq.Expressions;
  8. using System.Reflection;
  9. using System.Runtime.CompilerServices;
  10. using System.Windows.Input;
  11. using Aitex.Core.RT.IOCore;
  12. using Aitex.Core.Utilities;
  13. namespace Aitex.Core.UI.MVVM
  14. {
  15. public class NotifyPropertyChangedBase : INotifyPropertyChanged
  16. {
  17. public event PropertyChangedEventHandler PropertyChanged;
  18. protected void SetProperty<T>(ref T property, T value, [CallerMemberName] string memberName = "")
  19. {
  20. property = value;
  21. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(memberName));
  22. }
  23. }
  24. public class ViewModelBase : INotifyPropertyChanged, IViewModelControl
  25. {
  26. public event PropertyChangedEventHandler PropertyChanged;
  27. public void InvokePropertyChanged(string propertyName)
  28. {
  29. if (PropertyChanged != null)
  30. {
  31. PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  32. }
  33. }
  34. public void InvokeAllPropertyChanged()
  35. {
  36. PropertyInfo[] ps = this.GetType().GetProperties();
  37. foreach (PropertyInfo p in ps)
  38. {
  39. InvokePropertyChanged(p.Name);
  40. if (p.PropertyType == typeof(ICommand))
  41. {
  42. DelegateCommand<string> cmd = p.GetValue(this, null) as DelegateCommand<string>;
  43. if (cmd != null)
  44. cmd.RaiseCanExecuteChanged();
  45. }
  46. }
  47. FieldInfo[] fi = this.GetType().GetFields();
  48. foreach (FieldInfo p in fi)
  49. {
  50. InvokePropertyChanged(p.Name);
  51. if (p.FieldType == typeof(ICommand))
  52. {
  53. DelegateCommand<string> cmd = p.GetValue(this) as DelegateCommand<string>;
  54. if (cmd != null)
  55. cmd.RaiseCanExecuteChanged();
  56. }
  57. }
  58. //Parallel.ForEach(this.GetType().GetProperties(), property => InvokePropertyChanged(property.Name));
  59. }
  60. public void InvokePropertyChanged()
  61. {
  62. PropertyInfo[] ps = this.GetType().GetProperties();
  63. foreach (PropertyInfo p in ps)
  64. {
  65. if (!p.GetCustomAttributes(false).Any(attribute=>attribute is IgnorePropertyChangeAttribute))
  66. InvokePropertyChanged(p.Name);
  67. if (p.PropertyType == typeof(ICommand))
  68. {
  69. if (p.GetValue(this, null) is IDelegateCommand cmd)
  70. cmd.RaiseCanExecuteChanged();
  71. }
  72. }
  73. FieldInfo[] fi = this.GetType().GetFields();
  74. foreach (FieldInfo p in fi)
  75. {
  76. InvokePropertyChanged(p.Name);
  77. if (p.FieldType == typeof(ICommand))
  78. {
  79. DelegateCommand<string> cmd = p.GetValue(this) as DelegateCommand<string>;
  80. if (cmd != null)
  81. cmd.RaiseCanExecuteChanged();
  82. }
  83. }
  84. //Parallel.ForEach(this.GetType().GetProperties(), property => InvokePropertyChanged(property.Name));
  85. }
  86. }
  87. }