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

Can't focus NumericUpDown #8069

Closed
RivenSkaye opened this issue May 3, 2022 · 4 comments · Fixed by #13376
Closed

Can't focus NumericUpDown #8069

RivenSkaye opened this issue May 3, 2022 · 4 comments · Fixed by #13376
Labels
area-focus bug help-wanted A contribution from the community would be most welcome.

Comments

@RivenSkaye
Copy link

Describe the bug
NumericUpDown elements can not receive focus, but some of its internal children can and should.
Currently when calling NumericUpDown.Focus(), nothing happens with regard to visual and keyboard focus.
This seems like a mistake, as one would expect the TextBox inside the NumericUpDown template to become the focused object, with the caret being put inside of it, possibly selecting the numerical content of the box as well.

To Reproduce

  • Create a NumericUpDown element, either through code behind or XAML
  • Grab the element in the code behind, for example on the LayoutChanged element of the parent container
  • Call .Focus() on the element and watch as nothing happens.

Expected behavior
The focus and all keyboard input being put onto the TextBox inside of the NumericUpDown (like how calling .Focus() on a TextBox works), or the NumericUpDown element otherwise becoming focused and taking in user input.

Desktop (please complete the following information):

  • OS: Windows 10

Additional context
I've tried selecting its children elements directly, but I can't seem to access these from the code end of things.

@RivenSkaye RivenSkaye added the bug label May 3, 2022
@RivenSkaye
Copy link
Author

For anyone facing the same issue, with a bit of prodding around, I managed to find a hackjob solution that involves navigating the VisualTree starting from the Parent and casting the visual element to a Control.

public static void MyLayoutUpdatedHandler(object? sender, EventArgs? e){
    // The ItemsControl from a template
    var children = ((ItemsControl)sender).Presenter.Panel.LogicalChildren;
    // The panel I use to lay my objects out
    var pnl = (Panel)children[0].Child;
    // The NumericUpDown. This is where the fun begins!
    // For some reason, the VisualChildren Collection is not available on a Control, but it is on an IControl.
    // Could cast the nud to one, I guess, but I need it for other stuff too.
    Control nud = (Control)pnl.Children[^1];
    // It's safe to assume a Parent exists, considering there's always the Window above us
    ((Control)nud.Parent!
        // Grab the NumericUpDown again
        .VisualChildren[^1]
        // Then grab the ButtonSpinner,
        .VisualChildren[0]
        // The Border
        .VisualChildren[0]
        // The Grid
        .VisualChildren[0]
        // The ContentPresenter
        .VisualChildren[0]
        // And finally the TextBox
        .VisualChildren[0]
    ).Focus(); // Focus() on nud should just redirect the call here imo, but it doesn't...
}

@maxkatz6 maxkatz6 added the help-wanted A contribution from the community would be most welcome. label May 24, 2022
amwx added a commit to amwx/Avalonia that referenced this issue Jun 26, 2022
@jpmikkers
Copy link

jpmikkers commented Aug 8, 2022

probably the same issue: when clicking a Label with a NumericUpDown as target it does not result in a focused NumericUpDown element (avalonia 0.10.17)

@henry5059
Copy link

henry5059 commented Aug 24, 2023

Hi, I solved it with the following lines of code:

Control textBoxOfNumeric = numericUpDown1!.GetTemplateChildren().First(item => item.Name == "PART_TextBox"); textBoxOfNumeric.Focus();
I hope it works for you.

@timunie
Copy link
Contributor

timunie commented Aug 25, 2023

Better than everyone is adding the same hack would be a PR to fix the actual issue. The issue is labled with help wanted, so a PR is welcome

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-focus bug help-wanted A contribution from the community would be most welcome.
Projects
None yet
5 participants