Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TextBox text unselected when getting focus trough mouse click. #16825

Open
mateli opened this issue Aug 27, 2024 · 2 comments
Open

TextBox text unselected when getting focus trough mouse click. #16825

mateli opened this issue Aug 27, 2024 · 2 comments
Labels

Comments

@mateli
Copy link

mateli commented Aug 27, 2024

Describe the bug

If a textbox get focus by clicking then selected text is deselected.

To Reproduce

mybox.GotFocus += (_, _) => mybox.SelectAll();

Expected behavior

With the code above mybox should whenever it get focus select all text which it does. However it seems that when it get's focus from a pointer event it deselects all text and it does that after the focus event code has been run.

Avalonia version

11.1.3

OS

No response

Additional context

No response

@mateli mateli added the bug label Aug 27, 2024
@stevemonaco
Copy link
Contributor

stevemonaco commented Aug 28, 2024

You need to handle the pointer event so that the TextBox will not, at least for this narrow scenario (and allow it to handle other scenarios). I've written an attached behavior for this which should work for either clicking or any other focus-related trigger (like tab navigation).

public class TextBoxFocusSelectionBehavior : Behavior<TextBox>
{
    /// <inheritdoc />
    protected override void OnAttachedToVisualTree()
    {
        AssociatedObject?.AddHandler(InputElement.GotFocusEvent, AssociatedObject_GotFocus, RoutingStrategies.Bubble);
        AssociatedObject?.AddHandler(InputElement.PointerPressedEvent, AssociatedObject_PreviewPointerPressed, RoutingStrategies.Tunnel);
    }

    /// <inheritdoc />
    protected override void OnDetachedFromVisualTree()
    {
        AssociatedObject?.RemoveHandler(InputElement.GotFocusEvent, AssociatedObject_GotFocus);
        AssociatedObject?.RemoveHandler(InputElement.PointerPressedEvent, AssociatedObject_PreviewPointerPressed);
    }

    private void AssociatedObject_GotFocus(object? sender, GotFocusEventArgs e)
    {
        AssociatedObject?.SelectAll();
    }

    private void AssociatedObject_PreviewPointerPressed(object? sender, PointerPressedEventArgs e)
    {
        if (AssociatedObject?.IsKeyboardFocusWithin is false)
        {
            AssociatedObject.Focus();
            e.Handled = true;
        }
    }
}

If you don't wish to use behaviors, you can follow the event implementation above. You still need to subscribe to PointerPressed with a tunneling event (not a bubbling event like you're doing).

@rabbitism
Copy link
Contributor

related: #11693

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants