简体   繁体   中英

How to set a focus to TextBox in Avalonia

I have TextBox and Button, when I click Button, the text from TextBox sends to server. And TextBox loses focus. How i can set focus to the TextBox after Button click? I've read about FocusManager , but I don't understand how to use it((

I done it with Behaviors. Behavior class

    public class FocusOnPropertyChangedBehavior : Behavior<Control>
{
    protected override void OnAttached()
    {
        base.OnAttached();
        AssociatedObject.PropertyChanged += FocuseControl;
    }
    protected override void OnDetaching()
    {
        base.OnDetaching();
        AssociatedObject.PropertyChanged -= FocuseControl;
    }
    private void FocuseControl(object sender, AvaloniaPropertyChangedEventArgs e)
    {
        if (!AssociatedObject.IsFocused) AssociatedObject.Focus();
    }
}

Xaml code

    <i:Interaction.Behaviors>
        <custom:FocusOnPropertyChangedBehavior />
    </i:Interaction.Behaviors>

If you want to get focus only on start, use AttachedToVisualTree instead of PropertyChanged

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM