EventToCommand.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // reference from https://github.com/lbugnion/mvvmlight
  2. //The MIT License(MIT)
  3. //Copyright(c) 2009 - 2018 Laurent Bugnion
  4. //Permission is hereby granted, free of charge, to any person obtaining a copy
  5. //of this software and associated documentation files (the "Software"), to deal
  6. //in the Software without restriction, including without limitation the rights
  7. //to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. //copies of the Software, and to permit persons to whom the Software is
  9. //furnished to do so, subject to the following conditions:
  10. //The above copyright notice and this permission notice shall be included in all
  11. //copies or substantial portions of the Software.
  12. //THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. //IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. //FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. //AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. //LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. //OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. //SOFTWARE.
  19. using System;
  20. using System.Windows;
  21. using System.Windows.Input;
  22. using HandyControl.Data;
  23. namespace HandyControl.Interactivity;
  24. public class EventToCommand : TriggerAction<DependencyObject>
  25. {
  26. public const string EventArgsConverterParameterPropertyName = "EventArgsConverterParameter";
  27. public const string AlwaysInvokeCommandPropertyName = "AlwaysInvokeCommand";
  28. public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register(
  29. nameof(CommandParameter), typeof(object), typeof(EventToCommand), new PropertyMetadata(null,
  30. (s, e) =>
  31. {
  32. var eventToCommand = s as EventToCommand;
  33. if (eventToCommand?.AssociatedObject == null)
  34. return;
  35. eventToCommand.EnableDisableElement();
  36. }));
  37. public static readonly DependencyProperty CommandProperty =
  38. DependencyProperty.Register(nameof(Command), typeof(ICommand), typeof(EventToCommand),
  39. new PropertyMetadata(null, (s, e) => OnCommandChanged(s as EventToCommand, e)));
  40. public static readonly DependencyProperty MustToggleIsEnabledProperty = DependencyProperty.Register(
  41. nameof(MustToggleIsEnabled), typeof(bool), typeof(EventToCommand), new PropertyMetadata(ValueBoxes.FalseBox,
  42. (s, e) =>
  43. {
  44. var eventToCommand = s as EventToCommand;
  45. if (eventToCommand?.AssociatedObject == null)
  46. return;
  47. eventToCommand.EnableDisableElement();
  48. }));
  49. public static readonly DependencyProperty EventArgsConverterParameterProperty =
  50. DependencyProperty.Register(nameof(EventArgsConverterParameter), typeof(object),
  51. typeof(EventToCommand), new PropertyMetadata(null));
  52. public static readonly DependencyProperty AlwaysInvokeCommandProperty =
  53. DependencyProperty.Register(nameof(AlwaysInvokeCommand), typeof(bool), typeof(EventToCommand),
  54. new PropertyMetadata(ValueBoxes.FalseBox));
  55. private object _commandParameterValue;
  56. private bool? _mustToggleValue;
  57. public ICommand Command
  58. {
  59. get => (ICommand) GetValue(CommandProperty);
  60. set => SetValue(CommandProperty, value);
  61. }
  62. public object CommandParameter
  63. {
  64. get => GetValue(CommandParameterProperty);
  65. set => SetValue(CommandParameterProperty, value);
  66. }
  67. public object CommandParameterValue
  68. {
  69. get => _commandParameterValue ?? CommandParameter;
  70. set
  71. {
  72. _commandParameterValue = value;
  73. EnableDisableElement();
  74. }
  75. }
  76. public bool MustToggleIsEnabled
  77. {
  78. get => (bool) GetValue(MustToggleIsEnabledProperty);
  79. set => SetValue(MustToggleIsEnabledProperty, ValueBoxes.BooleanBox(value));
  80. }
  81. public bool MustToggleIsEnabledValue
  82. {
  83. get
  84. {
  85. if (_mustToggleValue.HasValue)
  86. return _mustToggleValue.Value;
  87. return MustToggleIsEnabled;
  88. }
  89. set
  90. {
  91. _mustToggleValue = value;
  92. EnableDisableElement();
  93. }
  94. }
  95. public bool PassEventArgsToCommand { get; set; }
  96. public IEventArgsConverter EventArgsConverter { get; set; }
  97. public object EventArgsConverterParameter
  98. {
  99. get => GetValue(EventArgsConverterParameterProperty);
  100. set => SetValue(EventArgsConverterParameterProperty, value);
  101. }
  102. public bool AlwaysInvokeCommand
  103. {
  104. get => (bool) GetValue(AlwaysInvokeCommandProperty);
  105. set => SetValue(AlwaysInvokeCommandProperty, ValueBoxes.BooleanBox(value));
  106. }
  107. protected override void OnAttached()
  108. {
  109. base.OnAttached();
  110. EnableDisableElement();
  111. }
  112. private FrameworkElement GetAssociatedObject() => AssociatedObject as FrameworkElement;
  113. private ICommand GetCommand() => Command;
  114. public void Invoke() => Invoke(null);
  115. protected override void Invoke(object parameter)
  116. {
  117. if (AssociatedElementIsDisabled() && !AlwaysInvokeCommand)
  118. return;
  119. var command = GetCommand();
  120. var parameter1 = CommandParameterValue;
  121. if (parameter1 == null && PassEventArgsToCommand)
  122. parameter1 = EventArgsConverter == null
  123. ? parameter
  124. : EventArgsConverter.Convert(parameter, EventArgsConverterParameter);
  125. if (command == null || !command.CanExecute(parameter1))
  126. return;
  127. command.Execute(parameter1);
  128. }
  129. private static void OnCommandChanged(EventToCommand element, DependencyPropertyChangedEventArgs e)
  130. {
  131. if (element == null)
  132. return;
  133. if (e.OldValue != null)
  134. ((ICommand) e.OldValue).CanExecuteChanged -= element.OnCommandCanExecuteChanged;
  135. var newValue = (ICommand) e.NewValue;
  136. if (newValue != null)
  137. newValue.CanExecuteChanged += element.OnCommandCanExecuteChanged;
  138. element.EnableDisableElement();
  139. }
  140. private bool AssociatedElementIsDisabled()
  141. {
  142. var associatedObject = GetAssociatedObject();
  143. if (AssociatedObject == null)
  144. return true;
  145. if (associatedObject != null)
  146. return !associatedObject.IsEnabled;
  147. return false;
  148. }
  149. private void EnableDisableElement()
  150. {
  151. var associatedObject = GetAssociatedObject();
  152. if (associatedObject == null)
  153. return;
  154. var command = GetCommand();
  155. if (!MustToggleIsEnabledValue || command == null)
  156. return;
  157. associatedObject.IsEnabled = command.CanExecute(CommandParameterValue);
  158. }
  159. private void OnCommandCanExecuteChanged(object sender, EventArgs e) => EnableDisableElement();
  160. }