FocusOnAttachedToVisualTreeBehavior.cs 492 B

1234567891011121314151617
  1. using Avalonia.Controls;
  2. using Avalonia.Threading;
  3. using Avalonia.Xaml.Interactivity;
  4. namespace Avalonia.Xaml.Interactions.Custom;
  5. /// <summary>
  6. /// Focuses the <see cref="IBehavior.AssociatedObject"/> when attached to visual tree.
  7. /// </summary>
  8. public class FocusOnAttachedToVisualTreeBehavior : StyledElementBehavior<Control>
  9. {
  10. /// <inheritdoc/>
  11. protected override void OnAttachedToVisualTree()
  12. {
  13. Dispatcher.UIThread.Post(() => AssociatedObject?.Focus());
  14. }
  15. }