diff --git a/src/Avalonia.Xaml.Interactions.Custom/Actions/SetEnabledAction.cs b/src/Avalonia.Xaml.Interactions.Custom/Actions/SetEnabledAction.cs new file mode 100644 index 000000000..f87ea3388 --- /dev/null +++ b/src/Avalonia.Xaml.Interactions.Custom/Actions/SetEnabledAction.cs @@ -0,0 +1,66 @@ +using Avalonia.Controls; +using Avalonia.Input; +using Avalonia.Xaml.Interactivity; + +namespace Avalonia.Xaml.Interactions.Custom; + +/// +/// Sets the property of a control when executed. +/// +public class SetEnabledAction : StyledElementAction +{ + /// + /// Identifies the avalonia property. + /// + public static readonly StyledProperty TargetControlProperty = + AvaloniaProperty.Register(nameof(TargetControl)); + + /// + /// Identifies the avalonia property. + /// + public static readonly StyledProperty IsEnabledValueProperty = + AvaloniaProperty.Register(nameof(IsEnabledValue), true); + + /// + /// Gets or sets the target control. This is an avalonia property. + /// + [ResolveByName] + public Control? TargetControl + { + get => GetValue(TargetControlProperty); + set => SetValue(TargetControlProperty, value); + } + + /// + /// Gets or sets the value to assign to . + /// This is an avalonia property. + /// + public bool IsEnabledValue + { + get => GetValue(IsEnabledValueProperty); + set => SetValue(IsEnabledValueProperty, value); + } + + /// + /// Executes the action. + /// + /// The that is passed to the action by the behavior. + /// The value of this parameter is determined by the caller. + /// True if the property is successfully updated; else false. + public override object Execute(object? sender, object? parameter) + { + if (!IsEnabled) + { + return false; + } + + var control = TargetControl ?? sender as Control; + if (control is null) + { + return false; + } + + control.SetCurrentValue(InputElement.IsEnabledProperty, IsEnabledValue); + return true; + } +}