ViewModelBase.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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
  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. DelegateCommand<string> cmd = p.GetValue(this, null) as DelegateCommand<string>;
  60. if (cmd != null)
  61. cmd.RaiseCanExecuteChanged();
  62. }
  63. }
  64. FieldInfo[] fi = this.GetType().GetFields();
  65. foreach (FieldInfo p in fi)
  66. {
  67. InvokePropertyChanged(p.Name);
  68. if (p.FieldType == typeof(ICommand))
  69. {
  70. DelegateCommand<string> cmd = p.GetValue(this) as DelegateCommand<string>;
  71. if (cmd != null)
  72. cmd.RaiseCanExecuteChanged();
  73. }
  74. }
  75. //Parallel.ForEach(this.GetType().GetProperties(), property => InvokePropertyChanged(property.Name));
  76. }
  77. }
  78. }