InvokeCommandAction.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System.Windows.Input;
  2. using Avalonia.Data.Converters;
  3. namespace Avalonia.Xaml.Interactions.Core;
  4. /// <summary>
  5. /// Executes a specified <see cref="System.Windows.Input.ICommand"/> when invoked.
  6. /// </summary>
  7. public class InvokeCommandAction : Interactivity.Action
  8. {
  9. /// <summary>
  10. /// Identifies the <seealso cref="Command"/> avalonia property.
  11. /// </summary>
  12. public static readonly StyledProperty<ICommand?> CommandProperty =
  13. AvaloniaProperty.Register<InvokeCommandAction, ICommand?>(nameof(Command));
  14. /// <summary>
  15. /// Identifies the <seealso cref="CommandParameter"/> avalonia property.
  16. /// </summary>
  17. public static readonly StyledProperty<object?> CommandParameterProperty =
  18. AvaloniaProperty.Register<InvokeCommandAction, object?>(nameof(CommandParameter));
  19. /// <summary>
  20. /// Identifies the <seealso cref="InputConverter"/> avalonia property.
  21. /// </summary>
  22. public static readonly StyledProperty<IValueConverter?> InputConverterProperty =
  23. AvaloniaProperty.Register<InvokeCommandAction, IValueConverter?>(nameof(InputConverter));
  24. /// <summary>
  25. /// Identifies the <seealso cref="InputConverterParameter"/> avalonia property.
  26. /// </summary>
  27. public static readonly StyledProperty<object?> InputConverterParameterProperty =
  28. AvaloniaProperty.Register<InvokeCommandAction, object?>(nameof(InputConverterParameter));
  29. /// <summary>
  30. /// Identifies the <seealso cref="InputConverterLanguage"/> avalonia property.
  31. /// </summary>
  32. /// <remarks>The string.Empty used for default value string means the invariant culture.</remarks>
  33. public static readonly StyledProperty<string?> InputConverterLanguageProperty =
  34. AvaloniaProperty.Register<InvokeCommandAction, string?>(nameof(InputConverterLanguage), string.Empty);
  35. /// <summary>
  36. /// Gets or sets the command this action should invoke. This is a avalonia property.
  37. /// </summary>
  38. public ICommand? Command
  39. {
  40. get => GetValue(CommandProperty);
  41. set => SetValue(CommandProperty, value);
  42. }
  43. /// <summary>
  44. /// Gets or sets the parameter that is passed to <see cref="System.Windows.Input.ICommand.Execute(object)"/>.
  45. /// If this is not set, the parameter from the <seealso cref="Execute(object, object)"/> method will be used.
  46. /// This is an optional avalonia property.
  47. /// </summary>
  48. public object? CommandParameter
  49. {
  50. get => GetValue(CommandParameterProperty);
  51. set => SetValue(CommandParameterProperty, value);
  52. }
  53. /// <summary>
  54. /// Gets or sets the converter that is run on the parameter from the <seealso cref="Execute(object, object)"/> method.
  55. /// This is an optional avalonia property.
  56. /// </summary>
  57. public IValueConverter? InputConverter
  58. {
  59. get => GetValue(InputConverterProperty);
  60. set => SetValue(InputConverterProperty, value);
  61. }
  62. /// <summary>
  63. /// Gets or sets the parameter that is passed to the <see cref="IValueConverter.Convert"/>
  64. /// method of <see cref="InputConverter"/>.
  65. /// This is an optional avalonia property.
  66. /// </summary>
  67. public object? InputConverterParameter
  68. {
  69. get => GetValue(InputConverterParameterProperty);
  70. set => SetValue(InputConverterParameterProperty, value);
  71. }
  72. /// <summary>
  73. /// Gets or sets the language that is passed to the <see cref="IValueConverter.Convert"/>
  74. /// method of <see cref="InputConverter"/>.
  75. /// This is an optional avalonia property.
  76. /// </summary>
  77. public string? InputConverterLanguage
  78. {
  79. get => GetValue(InputConverterLanguageProperty);
  80. set => SetValue(InputConverterLanguageProperty, value);
  81. }
  82. /// <summary>
  83. /// Specifies whether the EventArgs of the event that triggered this action should be passed to the Command as a parameter.
  84. /// </summary>
  85. public bool PassEventArgsToCommand { get; set; }
  86. /// <summary>
  87. /// Executes the action.
  88. /// </summary>
  89. /// <param name="sender">The <see cref="object"/> that is passed to the action by the behavior. Generally this is <seealso cref="Avalonia.Xaml.Interactivity.IBehavior.AssociatedObject"/> or a target object.</param>
  90. /// <param name="parameter">The value of this parameter is determined by the caller.</param>
  91. /// <returns>True if the command is successfully executed; else false.</returns>
  92. public override object Execute(object? sender, object? parameter)
  93. {
  94. if (IsEnabled != true || Command is null)
  95. {
  96. return false;
  97. }
  98. object? resolvedParameter = default;
  99. if (IsSet(CommandParameterProperty))
  100. {
  101. resolvedParameter = CommandParameter;
  102. }
  103. else if (InputConverter is not null)
  104. {
  105. resolvedParameter = InputConverter.Convert(
  106. parameter,
  107. typeof(object),
  108. InputConverterParameter,
  109. InputConverterLanguage is not null
  110. ?
  111. new System.Globalization.CultureInfo(InputConverterLanguage)
  112. : System.Globalization.CultureInfo.CurrentCulture);
  113. }
  114. else
  115. {
  116. if (PassEventArgsToCommand)
  117. {
  118. resolvedParameter = parameter;
  119. }
  120. }
  121. if (!Command.CanExecute(resolvedParameter))
  122. {
  123. return false;
  124. }
  125. Command.Execute(resolvedParameter);
  126. return true;
  127. }
  128. }