CommandTrigger.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 bool IsInitialized { get; set; }
  8. #region Dependency Properties
  9. #region Command Property
  10. /// <value>Identifies the Command dependency property</value>
  11. public static readonly DependencyProperty CommandProperty =
  12. DependencyProperty.Register("Command", typeof(ICommand), typeof(CommandTrigger),
  13. new FrameworkPropertyMetadata(null));
  14. /// <value>description for Command property</value>
  15. public ICommand Command
  16. {
  17. get { return (ICommand)GetValue(CommandProperty); }
  18. set { SetValue(CommandProperty, value); }
  19. }
  20. #endregion
  21. #region CustomParameter Property
  22. /// <value>Identifies the CustomParameterProperty dependency property</value>
  23. public static readonly DependencyProperty CustomParameterProperty =
  24. DependencyProperty.Register("CustomParameter", typeof(object), typeof(CommandTrigger),
  25. new FrameworkPropertyMetadata(null));
  26. /// <value>description for CustomParameter property</value>
  27. public object CustomParameter
  28. {
  29. get { return (object)GetValue(CustomParameterProperty); }
  30. set { SetValue(CustomParameterProperty, value); }
  31. }
  32. #endregion
  33. #region CommandTarget Property
  34. /// <value>Identifies the CommandTarget dependency property</value>
  35. public static readonly DependencyProperty CommandTargetProperty =
  36. DependencyProperty.Register("CommandTarget", typeof(IInputElement), typeof(CommandTrigger),
  37. new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.Inherits));
  38. /// <value>description for CommandTarget property</value>
  39. public IInputElement CommandTarget
  40. {
  41. get { return (IInputElement)GetValue(CommandTargetProperty); }
  42. set { SetValue(CommandTargetProperty, value); }
  43. }
  44. #endregion
  45. #endregion
  46. #region Internals
  47. void ICommandTrigger.Initialize(FrameworkElement source)
  48. {
  49. if (IsInitialized)
  50. return;
  51. InitializeCore(source);
  52. IsInitialized = true;
  53. }
  54. protected abstract void InitializeCore(FrameworkElement source);
  55. protected void ExecuteCommand(CommandParameter<object> parameter)
  56. {
  57. if (Command == null)
  58. return;
  59. RoutedCommand routedCommand = Command as RoutedCommand;
  60. if (routedCommand != null)
  61. {
  62. routedCommand.Execute(parameter, CommandTarget);
  63. }
  64. else
  65. {
  66. Command.Execute(parameter);
  67. }
  68. }
  69. #endregion
  70. }
  71. }