CustomCommandAction .cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using Microsoft.Xaml.Behaviors;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Input;
  9. namespace PDF_Office.CustomControl.SystemControl
  10. {
  11. /// <summary>
  12. /// 自动传送事件参数的辅助类
  13. /// </summary>
  14. public sealed class CustomCommandAction : TriggerAction<DependencyObject>
  15. {
  16. public static readonly DependencyProperty CommandParameterProperty =
  17. DependencyProperty.Register("CommandParameter", typeof(object), typeof(CustomCommandAction), null);
  18. public static readonly DependencyProperty CommandProperty = DependencyProperty.Register(
  19. "Command", typeof(ICommand), typeof(CustomCommandAction), null);
  20. public ICommand Command
  21. {
  22. get
  23. {
  24. return (ICommand)this.GetValue(CommandProperty);
  25. }
  26. set
  27. {
  28. this.SetValue(CommandProperty, value);
  29. }
  30. }
  31. public object CommandParameter
  32. {
  33. get
  34. {
  35. return this.GetValue(CommandParameterProperty);
  36. }
  37. set
  38. {
  39. this.SetValue(CommandParameterProperty, value);
  40. }
  41. }
  42. protected override void Invoke(object parameter)
  43. {
  44. if (this.AssociatedObject != null)
  45. {
  46. ICommand command = this.Command;
  47. if (command != null)
  48. {
  49. if (this.CommandParameter != null)
  50. {
  51. if (command.CanExecute(this.CommandParameter))
  52. {
  53. command.Execute(this.CommandParameter);
  54. }
  55. }
  56. else
  57. {
  58. if (command.CanExecute(parameter))
  59. {
  60. command.Execute(parameter);
  61. }
  62. }
  63. }
  64. }
  65. }
  66. }
  67. }