From 5b31f799188d8c887e264fb4ca22c5529a861b41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wies=C5=82aw=20=C5=A0olt=C3=A9s?= Date: Tue, 20 May 2025 10:52:41 +0200 Subject: [PATCH 1/2] Add SetEnabledAction to control IsEnabled --- .../Actions/SetEnabledAction.cs | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 src/Avalonia.Xaml.Interactions.Custom/Actions/SetEnabledAction.cs 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..3474fc3fe --- /dev/null +++ b/src/Avalonia.Xaml.Interactions.Custom/Actions/SetEnabledAction.cs @@ -0,0 +1,65 @@ +using Avalonia.Controls; +using Avalonia.Xaml.Interactivity; + +namespace Avalonia.Xaml.Interactions.Custom; + +/// +/// Sets the property of a control when executed. +/// +public class SetEnabledAction : Avalonia.Xaml.Interactivity.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.IsEnabled = IsEnabledValue; + return true; + } +} From da45b360699b2ef78506f42581d38e1147e04dc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wies=C5=82aw=20S=CC=8Colte=CC=81s?= Date: Tue, 20 May 2025 11:43:41 +0200 Subject: [PATCH 2/2] Update SetEnabledAction.cs --- .../Actions/SetEnabledAction.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Avalonia.Xaml.Interactions.Custom/Actions/SetEnabledAction.cs b/src/Avalonia.Xaml.Interactions.Custom/Actions/SetEnabledAction.cs index 3474fc3fe..f87ea3388 100644 --- a/src/Avalonia.Xaml.Interactions.Custom/Actions/SetEnabledAction.cs +++ b/src/Avalonia.Xaml.Interactions.Custom/Actions/SetEnabledAction.cs @@ -1,4 +1,5 @@ using Avalonia.Controls; +using Avalonia.Input; using Avalonia.Xaml.Interactivity; namespace Avalonia.Xaml.Interactions.Custom; @@ -6,7 +7,7 @@ namespace Avalonia.Xaml.Interactions.Custom; /// /// Sets the property of a control when executed. /// -public class SetEnabledAction : Avalonia.Xaml.Interactivity.StyledElementAction +public class SetEnabledAction : StyledElementAction { /// /// Identifies the avalonia property. @@ -59,7 +60,7 @@ public override object Execute(object? sender, object? parameter) return false; } - control.IsEnabled = IsEnabledValue; + control.SetCurrentValue(InputElement.IsEnabledProperty, IsEnabledValue); return true; } }