-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create FocusAutoCompleteBoxTextBoxBehavior.cs
- Loading branch information
1 parent
d961dbe
commit 0a71b3a
Showing
1 changed file
with
33 additions
and
0 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
src/Avalonia.Xaml.Interactions.Custom/AutoCompleteBox/FocusAutoCompleteBoxTextBoxBehavior.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |