ExecuteCommandBehaviorBase.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System.Windows.Input;
  2. using Avalonia.Controls;
  3. using Avalonia.Threading;
  4. using Avalonia.VisualTree;
  5. namespace Avalonia.Xaml.Interactions.Custom;
  6. /// <summary>
  7. ///
  8. /// </summary>
  9. public abstract class ExecuteCommandBehaviorBase : AttachedToVisualTreeBehavior<Control>
  10. {
  11. /// <summary>
  12. ///
  13. /// </summary>
  14. public static readonly StyledProperty<ICommand?> CommandProperty =
  15. AvaloniaProperty.Register<ExecuteCommandBehaviorBase, ICommand?>(nameof(Command));
  16. /// <summary>
  17. ///
  18. /// </summary>
  19. public static readonly StyledProperty<object?> CommandParameterProperty =
  20. AvaloniaProperty.Register<ExecuteCommandBehaviorBase, object?>(nameof(CommandParameter));
  21. /// <summary>
  22. ///
  23. /// </summary>
  24. public static readonly StyledProperty<bool> FocusTopLevelProperty =
  25. AvaloniaProperty.Register<ExecuteCommandBehaviorBase, bool>(nameof(FocusTopLevel));
  26. /// <summary>
  27. ///
  28. /// </summary>
  29. public static readonly StyledProperty<Control?> FocusControlProperty =
  30. AvaloniaProperty.Register<ExecuteCommandBehaviorBase, Control?>(nameof(CommandParameter));
  31. /// <summary>
  32. ///
  33. /// </summary>
  34. public static readonly StyledProperty<Control?> SourceControlProperty =
  35. AvaloniaProperty.Register<ExecuteCommandBehaviorBase, Control?>(nameof(SourceControl));
  36. /// <summary>
  37. ///
  38. /// </summary>
  39. public ICommand? Command
  40. {
  41. get => GetValue(CommandProperty);
  42. set => SetValue(CommandProperty, value);
  43. }
  44. /// <summary>
  45. ///
  46. /// </summary>
  47. public object? CommandParameter
  48. {
  49. get => GetValue(CommandParameterProperty);
  50. set => SetValue(CommandParameterProperty, value);
  51. }
  52. /// <summary>
  53. ///
  54. /// </summary>
  55. public bool FocusTopLevel
  56. {
  57. get => GetValue(FocusTopLevelProperty);
  58. set => SetValue(FocusTopLevelProperty, value);
  59. }
  60. /// <summary>
  61. ///
  62. /// </summary>
  63. [ResolveByName]
  64. public Control? FocusControl
  65. {
  66. get => GetValue(FocusControlProperty);
  67. set => SetValue(FocusControlProperty, value);
  68. }
  69. [ResolveByName]
  70. public Control? SourceControl
  71. {
  72. get => GetValue(SourceControlProperty);
  73. set => SetValue(SourceControlProperty, value);
  74. }
  75. /// <summary>
  76. ///
  77. /// </summary>
  78. /// <returns></returns>
  79. protected virtual bool ExecuteCommand()
  80. {
  81. if (!IsEnabled)
  82. {
  83. return false;
  84. }
  85. if (AssociatedObject is not { IsVisible: true, IsEnabled: true })
  86. {
  87. return false;
  88. }
  89. if (Command?.CanExecute(CommandParameter) != true)
  90. {
  91. return false;
  92. }
  93. if (FocusTopLevel)
  94. {
  95. Dispatcher.UIThread.Post(() => (AssociatedObject?.GetVisualRoot() as TopLevel)?.Focus());
  96. }
  97. if (FocusControl is { } focusControl)
  98. {
  99. Dispatcher.UIThread.Post(() => focusControl.Focus());
  100. }
  101. Command.Execute(CommandParameter);
  102. return true;
  103. }
  104. }