Skip to content

Commit

Permalink
Create FocusAutoCompleteBoxTextBoxBehavior.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
wieslawsoltes committed Nov 19, 2024
1 parent d961dbe commit 0a71b3a
Showing 1 changed file with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Linq;
using System.Reactive.Disposables;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Threading;
using Avalonia.VisualTree;

namespace Avalonia.Xaml.Interactions.Custom;

/// <summary>
///
/// </summary>
public class FocusAutoCompleteBoxTextBoxBehavior : AttachedToVisualTreeBehavior<AutoCompleteBox>
{
/// <inheritdoc />
protected override void OnAttachedToVisualTree(CompositeDisposable disposable)
{
if (AssociatedObject is null)
{
return;
}

AssociatedObject.GotFocus += AssociatedObjectOnGotFocus;

Disposable.Create(() => AssociatedObject.GotFocus -= AssociatedObjectOnGotFocus);
}

private void AssociatedObjectOnGotFocus(object? sender, GotFocusEventArgs e)
{
var textBox = AssociatedObject?.GetVisualDescendants().OfType<TextBox>().FirstOrDefault();
Dispatcher.UIThread.Post(() => textBox?.Focus());
}
}

0 comments on commit 0a71b3a

Please sign in to comment.