终端一体化运控平台
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 

174 wiersze
4.2 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows.Input;
  7. namespace BPASmartClient.SCADAControl.Converters
  8. {
  9. public class RelayCommandSimple :ICommand, IDisposable
  10. {
  11. private Action _executeCallback;
  12. private Func<bool> _canExecute;
  13. public RelayCommandSimple(Action execute)
  14. {
  15. _executeCallback = execute;
  16. }
  17. public RelayCommandSimple(Action execute,Func<bool> canExecute)
  18. : this(execute)
  19. {
  20. _canExecute = canExecute;
  21. }
  22. public bool CanExecute(object parameter)
  23. {
  24. if (_canExecute == null)
  25. return true;
  26. return _canExecute();
  27. }
  28. public void Execute(object parameter)
  29. {
  30. _executeCallback();
  31. }
  32. public void Dispose()
  33. {
  34. _executeCallback = null;
  35. _canExecute = null;
  36. }
  37. public event EventHandler CanExecuteChanged
  38. {
  39. add
  40. {
  41. if (this._canExecute != null)
  42. {
  43. CommandManager.RequerySuggested += value;
  44. }
  45. }
  46. remove
  47. {
  48. if (this._canExecute != null)
  49. {
  50. CommandManager.RequerySuggested -= value;
  51. }
  52. }
  53. }
  54. }
  55. public class RelayCommandSimple<TParam> :ICommand
  56. {
  57. private Action<TParam> _executeCallback;
  58. private Func<TParam,bool> _canExecute;
  59. public RelayCommandSimple(Action<TParam> execute)
  60. {
  61. if (execute == null)
  62. throw new ArgumentNullException("execute");
  63. _executeCallback = execute;
  64. }
  65. public RelayCommandSimple(Action<TParam> execute,Func<TParam,bool> canExecute)
  66. : this(execute)
  67. {
  68. _canExecute = canExecute;
  69. }
  70. public bool CanExecute(object parameter)
  71. {
  72. if (_canExecute == null)
  73. return true;
  74. if (parameter != null && parameter is TParam)
  75. {
  76. return _canExecute((TParam)parameter);
  77. }
  78. return true;
  79. }
  80. public void Execute(object parameter)
  81. {
  82. if (parameter != null && parameter is TParam)
  83. {
  84. _executeCallback((TParam)parameter);
  85. }
  86. }
  87. public event EventHandler CanExecuteChanged
  88. {
  89. add
  90. {
  91. if (this._canExecute != null)
  92. {
  93. CommandManager.RequerySuggested += value;
  94. }
  95. }
  96. remove
  97. {
  98. if (this._canExecute != null)
  99. {
  100. CommandManager.RequerySuggested -= value;
  101. }
  102. }
  103. }
  104. }
  105. public class RelayCommandSimpleNull<T> :ICommand
  106. {
  107. private Action<T> _executeCallback;
  108. private Func<T,bool> _canExecute;
  109. public RelayCommandSimpleNull(Action<T> execute)
  110. {
  111. if (execute == null)
  112. throw new ArgumentNullException("execute");
  113. _executeCallback = execute;
  114. }
  115. public RelayCommandSimpleNull(Action<T> execute,Func<T,bool> canExecute)
  116. : this(execute)
  117. {
  118. _canExecute = canExecute;
  119. }
  120. public bool CanExecute(object parameter)
  121. {
  122. if (_canExecute == null)
  123. return true;
  124. return _canExecute((T)parameter);
  125. }
  126. public void Execute(object parameter)
  127. {
  128. _executeCallback((T)parameter);
  129. }
  130. public event EventHandler CanExecuteChanged
  131. {
  132. add
  133. {
  134. if (this._canExecute != null)
  135. {
  136. CommandManager.RequerySuggested += value;
  137. }
  138. }
  139. remove
  140. {
  141. if (this._canExecute != null)
  142. {
  143. CommandManager.RequerySuggested -= value;
  144. }
  145. }
  146. }
  147. }
  148. }