using Avalonia.Controls;
using Avalonia.Threading;
using Avalonia.Xaml.Interactivity;
namespace Avalonia.Xaml.Interactions.Custom;
///
/// Focuses the associated or target control when executed.
///
public class FocusControlAction : Avalonia.Xaml.Interactivity.Action
{
///
/// Identifies the avalonia property.
///
public static readonly StyledProperty TargetControlProperty =
AvaloniaProperty.Register(nameof(TargetControl));
///
/// Gets or sets the target control. This is a avalonia property.
///
[ResolveByName]
public Control? TargetControl
{
get => GetValue(TargetControlProperty);
set => SetValue(TargetControlProperty, value);
}
///
/// Executes the action.
///
/// The that is passed to the action by the behavior. Generally this is or a target object.
/// The value of this parameter is determined by the caller.
/// Returns null after executed.
public override object? Execute(object? sender, object? parameter)
{
if (!IsEnabled)
{
return false;
}
var control = TargetControl ?? sender as Control;
Dispatcher.UIThread.Post(() => control?.Focus());
return null;
}
}