question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

BUG: AppBarButton click doesn't trigger focus loss on previously focused inputs

See original GitHub issue

// EDIT: initially this was a proposal and it unfolded into a bug. To get the full context please read all comments

Proposal: NumberBox NumberChanged event

Summary

There is currently no way to get value updates from a NumberBox just-in-time as the user types (like you can with a TextBox.TexChanged event).

Rationale

Given the summary above, this forces developers to make workarounds to certain scenarios. Example scenario: We want to input a value and save the value to a file

  • Given a form with a NumberBox having a Value={x:Bind MyProp, Mode=2way} and ValueChanged="OnValChanged"
  • Given a Button with Click="OnClick" which passes MyProp to a method
  • When I input a number in the NumberBox, and directly after that I click the Button
  • The OnClick triggers before the NumberBox change is picked up, thus passing an outdated value around.

In the case of a TextBox this annoying behavior can be worked around by having a TextChanged event handler which always updates MyProp just-in-time to be ready for being passed to the method invoked by OnClick In the case of a NumberBox, there’s no way to work around this, other than redesigning the UI to have a modal which collects the NumberBox value, and then passes it to the called to be used.

Scope

Capability Priority
Just-in-time NumberBox value changes Must

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:17 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
ccornicicommented, Apr 14, 2020

I wonder if @ranjeshj saw you tagging him above @chingucoding 😄

1reaction
chingucodingcommented, Apr 11, 2020

In the repo above, in MainPage.xaml, if I remove the ValueChanged=“NumberBox_ValueChanged”, the only way that MainPageViewModel.Amount will be updated now is through the x:Bind present in the xaml.

Given the above, I would expect that when I click the Button (not AppBarButton, which apparently has problems), the Amount is first updated since the NumberBox lost focus, and only then the Click event fires. But the opposite happens. The Click triggers first and the binding update triggers after.

Is this by design?

I think it is, but I am not really sure when bindings get evaluated. It may be a bug, but I think it’s more based on the way bindings work in XAML. But again I am not sure on that.

I tried setting focus on pretty much all elements in that XAML and it doesn’t work. Then I tried unsetting focus on the NumBox and it throws an exception as follows:

Hmm, it seems that setting the focus inside the event handler only raises ValueChanged after it finished the handler it is in. There are two possible workarounds for your situation now I think:

  1. Set flag (e.g. ClickedSendAppBar) inside AppBarButton click function, and change focus. Inside ValueChanged check for that flag and if it is set, do what you would like to do, in the example app send money.
  2. You can find the textbox manually in the UIA tree. For that you could use the following code:
public MainPage()
{
    this.InitializeComponent();
    var NumBoxFirstChild = VisualTreeHelper.GetChild(Numbox, 0);

    Loaded += MainPage_Loaded;
}

private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
    var numBoxRootGrid = VisualTreeHelper.GetChild(Numbox,0);
    // If you set header, it's 1, otherwise on initial load it is 0
    var inputBox = VisualTreeHelper.GetChild(numBoxRootGrid, 0) as TextBox;
    if(inputBox == null)
    {
        inputBox = VisualTreeHelper.GetChild(numBoxRootGrid, 1) as TextBox;
    }
    inputBox.TextChanged += InputBox_TextChanged;
}

private void InputBox_TextChanged(object sender, TextChangedEventArgs e)
{
    Debug.WriteLine($"Value changed to {(sender as TextBox).Text}");
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Click on a button that is moved when an input loses focus ...
When the input is focused and the user tries to click the button, it loses the focus which may cause the hint to...
Read more >
Focus and text fields
When a text field is selected and accepting input, it is said to have “focus.” Generally, users shift focus to a text field...
Read more >
Focusing: focus/blur
The focus event is called on focusing, and blur – when the element loses the focus. Let's use them for validation of an...
Read more >
UWP - Focus issues when opening a flyout from an ...
When opening a Flyout from an AppBarButton, the controls inside the flyout lose their ability to be focused when clicked/tapped (tab still ...
Read more >
Game loses focus : r/RocketLeague
Go to windows settings/gaming/game mode -> turn it off. Didn't work for me, though xD Rocket League lost focus precisely after 5 minutes....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found