ViewModelBase.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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.Windows.Input;
  10. using Aitex.Core.RT.IOCore;
  11. using Aitex.Core.Utilities;
  12. namespace Aitex.Core.UI.MVVM
  13. {
  14. public class ViewModelBase : INotifyPropertyChanged, IViewModelControl
  15. {
  16. public event PropertyChangedEventHandler PropertyChanged;
  17. public void InvokePropertyChanged(string propertyName)
  18. {
  19. if (PropertyChanged != null)
  20. {
  21. PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  22. }
  23. }
  24. public void InvokeAllPropertyChanged()
  25. {
  26. PropertyInfo[] ps = this.GetType().GetProperties();
  27. foreach (PropertyInfo p in ps)
  28. {
  29. InvokePropertyChanged(p.Name);
  30. if (p.PropertyType == typeof(ICommand))
  31. {
  32. DelegateCommand<string> cmd = p.GetValue(this, null) as DelegateCommand<string>;
  33. if (cmd != null)
  34. cmd.RaiseCanExecuteChanged();
  35. }
  36. }
  37. FieldInfo[] fi = this.GetType().GetFields();
  38. foreach (FieldInfo p in fi)
  39. {
  40. InvokePropertyChanged(p.Name);
  41. if (p.FieldType == typeof(ICommand))
  42. {
  43. DelegateCommand<string> cmd = p.GetValue(this) as DelegateCommand<string>;
  44. if (cmd != null)
  45. cmd.RaiseCanExecuteChanged();
  46. }
  47. }
  48. //Parallel.ForEach(this.GetType().GetProperties(), property => InvokePropertyChanged(property.Name));
  49. }
  50. public void InvokePropertyChanged()
  51. {
  52. PropertyInfo[] ps = this.GetType().GetProperties();
  53. foreach (PropertyInfo p in ps)
  54. {
  55. if (!p.GetCustomAttributes(false).Any(attribute=>attribute is IgnorePropertyChangeAttribute))
  56. InvokePropertyChanged(p.Name);
  57. if (p.PropertyType == typeof(ICommand))
  58. {
  59. if (p.GetValue(this, null) is IDelegateCommand cmd)
  60. cmd.RaiseCanExecuteChanged();
  61. }
  62. }
  63. FieldInfo[] fi = this.GetType().GetFields();
  64. foreach (FieldInfo p in fi)
  65. {
  66. InvokePropertyChanged(p.Name);
  67. if (p.FieldType == typeof(ICommand))
  68. {
  69. DelegateCommand<string> cmd = p.GetValue(this) as DelegateCommand<string>;
  70. if (cmd != null)
  71. cmd.RaiseCanExecuteChanged();
  72. }
  73. }
  74. //Parallel.ForEach(this.GetType().GetProperties(), property => InvokePropertyChanged(property.Name));
  75. }
  76. }
  77. }