123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- using System.Windows.Input;
- using Avalonia.Data.Converters;
- namespace Avalonia.Xaml.Interactions.Core;
- /// <summary>
- /// Executes a specified <see cref="System.Windows.Input.ICommand"/> when invoked.
- /// </summary>
- public class InvokeCommandAction : Interactivity.Action
- {
- /// <summary>
- /// Identifies the <seealso cref="Command"/> avalonia property.
- /// </summary>
- public static readonly StyledProperty<ICommand?> CommandProperty =
- AvaloniaProperty.Register<InvokeCommandAction, ICommand?>(nameof(Command));
- /// <summary>
- /// Identifies the <seealso cref="CommandParameter"/> avalonia property.
- /// </summary>
- public static readonly StyledProperty<object?> CommandParameterProperty =
- AvaloniaProperty.Register<InvokeCommandAction, object?>(nameof(CommandParameter));
- /// <summary>
- /// Identifies the <seealso cref="InputConverter"/> avalonia property.
- /// </summary>
- public static readonly StyledProperty<IValueConverter?> InputConverterProperty =
- AvaloniaProperty.Register<InvokeCommandAction, IValueConverter?>(nameof(InputConverter));
- /// <summary>
- /// Identifies the <seealso cref="InputConverterParameter"/> avalonia property.
- /// </summary>
- public static readonly StyledProperty<object?> InputConverterParameterProperty =
- AvaloniaProperty.Register<InvokeCommandAction, object?>(nameof(InputConverterParameter));
- /// <summary>
- /// Identifies the <seealso cref="InputConverterLanguage"/> avalonia property.
- /// </summary>
- /// <remarks>The string.Empty used for default value string means the invariant culture.</remarks>
- public static readonly StyledProperty<string?> InputConverterLanguageProperty =
- AvaloniaProperty.Register<InvokeCommandAction, string?>(nameof(InputConverterLanguage), string.Empty);
- /// <summary>
- /// Gets or sets the command this action should invoke. This is a avalonia property.
- /// </summary>
- public ICommand? Command
- {
- get => GetValue(CommandProperty);
- set => SetValue(CommandProperty, value);
- }
- /// <summary>
- /// Gets or sets the parameter that is passed to <see cref="System.Windows.Input.ICommand.Execute(object)"/>.
- /// If this is not set, the parameter from the <seealso cref="Execute(object, object)"/> method will be used.
- /// This is an optional avalonia property.
- /// </summary>
- public object? CommandParameter
- {
- get => GetValue(CommandParameterProperty);
- set => SetValue(CommandParameterProperty, value);
- }
- /// <summary>
- /// Gets or sets the converter that is run on the parameter from the <seealso cref="Execute(object, object)"/> method.
- /// This is an optional avalonia property.
- /// </summary>
- public IValueConverter? InputConverter
- {
- get => GetValue(InputConverterProperty);
- set => SetValue(InputConverterProperty, value);
- }
- /// <summary>
- /// Gets or sets the parameter that is passed to the <see cref="IValueConverter.Convert"/>
- /// method of <see cref="InputConverter"/>.
- /// This is an optional avalonia property.
- /// </summary>
- public object? InputConverterParameter
- {
- get => GetValue(InputConverterParameterProperty);
- set => SetValue(InputConverterParameterProperty, value);
- }
- /// <summary>
- /// Gets or sets the language that is passed to the <see cref="IValueConverter.Convert"/>
- /// method of <see cref="InputConverter"/>.
- /// This is an optional avalonia property.
- /// </summary>
- public string? InputConverterLanguage
- {
- get => GetValue(InputConverterLanguageProperty);
- set => SetValue(InputConverterLanguageProperty, value);
- }
- /// <summary>
- /// Specifies whether the EventArgs of the event that triggered this action should be passed to the Command as a parameter.
- /// </summary>
- public bool PassEventArgsToCommand { get; set; }
- /// <summary>
- /// Executes the action.
- /// </summary>
- /// <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>
- /// <param name="parameter">The value of this parameter is determined by the caller.</param>
- /// <returns>True if the command is successfully executed; else false.</returns>
- public override object Execute(object? sender, object? parameter)
- {
- if (IsEnabled != true || Command is null)
- {
- return false;
- }
- object? resolvedParameter = default;
- if (IsSet(CommandParameterProperty))
- {
- resolvedParameter = CommandParameter;
- }
- else if (InputConverter is not null)
- {
- resolvedParameter = InputConverter.Convert(
- parameter,
- typeof(object),
- InputConverterParameter,
- InputConverterLanguage is not null
- ?
- new System.Globalization.CultureInfo(InputConverterLanguage)
- : System.Globalization.CultureInfo.CurrentCulture);
- }
- else
- {
- if (PassEventArgsToCommand)
- {
- resolvedParameter = parameter;
- }
- }
- if (!Command.CanExecute(resolvedParameter))
- {
- return false;
- }
- Command.Execute(resolvedParameter);
- return true;
- }
- }
|