You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CommandHelpers.cs 3.6 KiB

1 vuosi sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System.Security;
  2. using System.Windows.Input;
  3. using System.Windows;
  4. namespace BPA.UIControl
  5. {
  6. internal static class CommandHelpers
  7. {
  8. internal static bool CanExecuteCommandSource(ICommandSource commandSource)
  9. {
  10. var command = commandSource.Command;
  11. if (command == null)
  12. {
  13. return false;
  14. }
  15. var commandParameter = commandSource.CommandParameter ?? commandSource;
  16. if (command is RoutedCommand routedCommand)
  17. {
  18. var target = commandSource.CommandTarget ?? commandSource as IInputElement;
  19. return routedCommand.CanExecute(commandParameter, target);
  20. }
  21. return command.CanExecute(commandParameter);
  22. }
  23. [SecurityCritical]
  24. [SecuritySafeCritical]
  25. internal static void ExecuteCommandSource(ICommandSource commandSource)
  26. {
  27. CriticalExecuteCommandSource(commandSource);
  28. }
  29. [SecurityCritical]
  30. internal static void CriticalExecuteCommandSource(ICommandSource commandSource)
  31. {
  32. var command = commandSource.Command;
  33. if (command == null)
  34. {
  35. return;
  36. }
  37. var commandParameter = commandSource.CommandParameter ?? commandSource;
  38. if (command is RoutedCommand routedCommand)
  39. {
  40. var target = commandSource.CommandTarget ?? commandSource as IInputElement;
  41. if (routedCommand.CanExecute(commandParameter, target))
  42. {
  43. routedCommand.Execute(commandParameter, target);
  44. }
  45. }
  46. else
  47. {
  48. if (command.CanExecute(commandParameter))
  49. {
  50. command.Execute(commandParameter);
  51. }
  52. }
  53. }
  54. internal static bool CanExecuteCommandSource(ICommandSource commandSource, ICommand theCommand)
  55. {
  56. var command = theCommand;
  57. if (command == null)
  58. {
  59. return false;
  60. }
  61. var commandParameter = commandSource.CommandParameter ?? commandSource;
  62. if (command is RoutedCommand routedCommand)
  63. {
  64. var target = commandSource.CommandTarget ?? commandSource as IInputElement;
  65. return routedCommand.CanExecute(commandParameter, target);
  66. }
  67. return command.CanExecute(commandParameter);
  68. }
  69. [SecurityCritical]
  70. [SecuritySafeCritical]
  71. internal static void ExecuteCommandSource(ICommandSource commandSource, ICommand theCommand)
  72. {
  73. CriticalExecuteCommandSource(commandSource, theCommand);
  74. }
  75. [SecurityCritical]
  76. internal static void CriticalExecuteCommandSource(ICommandSource commandSource, ICommand theCommand)
  77. {
  78. var command = theCommand;
  79. if (command == null)
  80. {
  81. return;
  82. }
  83. var commandParameter = commandSource.CommandParameter ?? commandSource;
  84. if (command is RoutedCommand routedCommand)
  85. {
  86. var target = commandSource.CommandTarget ?? commandSource as IInputElement;
  87. if (routedCommand.CanExecute(commandParameter, target))
  88. {
  89. routedCommand.Execute(commandParameter, target);
  90. }
  91. }
  92. else
  93. {
  94. if (command.CanExecute(commandParameter))
  95. {
  96. command.Execute(commandParameter);
  97. }
  98. }
  99. }
  100. }
  101. }