CommandTrigger.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System.Windows;
  2. using System.Windows.Input;
  3. namespace OpenSEMI.ClientBase.Command
  4. {
  5. public abstract class CommandTrigger : Freezable, ICommandTrigger
  6. {
  7. public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(CommandTrigger), new FrameworkPropertyMetadata(null));
  8. public static readonly DependencyProperty CustomParameterProperty = DependencyProperty.Register("CustomParameter", typeof(object), typeof(CommandTrigger), new FrameworkPropertyMetadata(null));
  9. public static readonly DependencyProperty CommandTargetProperty = DependencyProperty.Register("CommandTarget", typeof(IInputElement), typeof(CommandTrigger), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.Inherits));
  10. public bool IsInitialized
  11. {
  12. get;
  13. set;
  14. }
  15. public ICommand Command
  16. {
  17. get
  18. {
  19. return (ICommand)GetValue(CommandProperty);
  20. }
  21. set
  22. {
  23. SetValue(CommandProperty, value);
  24. }
  25. }
  26. public object CustomParameter
  27. {
  28. get
  29. {
  30. return GetValue(CustomParameterProperty);
  31. }
  32. set
  33. {
  34. SetValue(CustomParameterProperty, value);
  35. }
  36. }
  37. public IInputElement CommandTarget
  38. {
  39. get
  40. {
  41. return (IInputElement)GetValue(CommandTargetProperty);
  42. }
  43. set
  44. {
  45. SetValue(CommandTargetProperty, value);
  46. }
  47. }
  48. void ICommandTrigger.Initialize(FrameworkElement source)
  49. {
  50. if (!IsInitialized)
  51. {
  52. InitializeCore(source);
  53. IsInitialized = true;
  54. }
  55. }
  56. protected abstract void InitializeCore(FrameworkElement source);
  57. protected void ExecuteCommand(CommandParameter<object> parameter)
  58. {
  59. if (Command != null)
  60. {
  61. RoutedCommand routedCommand = Command as RoutedCommand;
  62. if (routedCommand != null)
  63. {
  64. routedCommand.Execute(parameter, CommandTarget);
  65. }
  66. else
  67. {
  68. Command.Execute(parameter);
  69. }
  70. }
  71. }
  72. }
  73. }