123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- using System.Windows.Input;
- using Avalonia.Controls;
- using Avalonia.Threading;
- using Avalonia.VisualTree;
- namespace Avalonia.Xaml.Interactions.Custom;
- /// <summary>
- ///
- /// </summary>
- public abstract class ExecuteCommandBehaviorBase : AttachedToVisualTreeBehavior<Control>
- {
- /// <summary>
- ///
- /// </summary>
- public static readonly StyledProperty<ICommand?> CommandProperty =
- AvaloniaProperty.Register<ExecuteCommandBehaviorBase, ICommand?>(nameof(Command));
- /// <summary>
- ///
- /// </summary>
- public static readonly StyledProperty<object?> CommandParameterProperty =
- AvaloniaProperty.Register<ExecuteCommandBehaviorBase, object?>(nameof(CommandParameter));
- /// <summary>
- ///
- /// </summary>
- public static readonly StyledProperty<bool> FocusTopLevelProperty =
- AvaloniaProperty.Register<ExecuteCommandBehaviorBase, bool>(nameof(FocusTopLevel));
- /// <summary>
- ///
- /// </summary>
- public static readonly StyledProperty<Control?> FocusControlProperty =
- AvaloniaProperty.Register<ExecuteCommandBehaviorBase, Control?>(nameof(CommandParameter));
-
- /// <summary>
- ///
- /// </summary>
- public static readonly StyledProperty<Control?> SourceControlProperty =
- AvaloniaProperty.Register<ExecuteCommandBehaviorBase, Control?>(nameof(SourceControl));
- /// <summary>
- ///
- /// </summary>
- public ICommand? Command
- {
- get => GetValue(CommandProperty);
- set => SetValue(CommandProperty, value);
- }
- /// <summary>
- ///
- /// </summary>
- public object? CommandParameter
- {
- get => GetValue(CommandParameterProperty);
- set => SetValue(CommandParameterProperty, value);
- }
- /// <summary>
- ///
- /// </summary>
- public bool FocusTopLevel
- {
- get => GetValue(FocusTopLevelProperty);
- set => SetValue(FocusTopLevelProperty, value);
- }
- /// <summary>
- ///
- /// </summary>
- [ResolveByName]
- public Control? FocusControl
- {
- get => GetValue(FocusControlProperty);
- set => SetValue(FocusControlProperty, value);
- }
- [ResolveByName]
- public Control? SourceControl
- {
- get => GetValue(SourceControlProperty);
- set => SetValue(SourceControlProperty, value);
- }
- /// <summary>
- ///
- /// </summary>
- /// <returns></returns>
- protected virtual bool ExecuteCommand()
- {
- if (!IsEnabled)
- {
- return false;
- }
- if (AssociatedObject is not { IsVisible: true, IsEnabled: true })
- {
- return false;
- }
- if (Command?.CanExecute(CommandParameter) != true)
- {
- return false;
- }
- if (FocusTopLevel)
- {
- Dispatcher.UIThread.Post(() => (AssociatedObject?.GetVisualRoot() as TopLevel)?.Focus());
- }
- if (FocusControl is { } focusControl)
- {
- Dispatcher.UIThread.Post(() => focusControl.Focus());
- }
- Command.Execute(CommandParameter);
- return true;
- }
- }
|