FocusBehaviorBase.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using Avalonia.Controls;
  2. using Avalonia.Input;
  3. using Avalonia.Threading;
  4. namespace Avalonia.Xaml.Interactions.Custom;
  5. /// <summary>
  6. ///
  7. /// </summary>
  8. public abstract class FocusBehaviorBase : AttachedToVisualTreeBehavior<Control>
  9. {
  10. /// <summary>
  11. ///
  12. /// </summary>
  13. public static readonly StyledProperty<NavigationMethod> NavigationMethodProperty =
  14. AvaloniaProperty.Register<FocusBehaviorBase, NavigationMethod>(nameof(NavigationMethod));
  15. /// <summary>
  16. ///
  17. /// </summary>
  18. public static readonly StyledProperty<KeyModifiers> KeyModifiersProperty =
  19. AvaloniaProperty.Register<FocusBehaviorBase, KeyModifiers>(nameof(KeyModifiers));
  20. /// <summary>
  21. ///
  22. /// </summary>
  23. public NavigationMethod NavigationMethod
  24. {
  25. get => GetValue(NavigationMethodProperty);
  26. set => SetValue(NavigationMethodProperty, value);
  27. }
  28. /// <summary>
  29. ///
  30. /// </summary>
  31. public KeyModifiers KeyModifiers
  32. {
  33. get => GetValue(KeyModifiersProperty);
  34. set => SetValue(KeyModifiersProperty, value);
  35. }
  36. /// <summary>
  37. ///
  38. /// </summary>
  39. /// <returns></returns>
  40. protected virtual bool Focus()
  41. {
  42. if (!IsEnabled)
  43. {
  44. return false;
  45. }
  46. Dispatcher.UIThread.Post(() => AssociatedObject?.Focus(NavigationMethod, KeyModifiers));
  47. return true;
  48. }
  49. }