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.

Provide an API for determining if the "main" component of a control has focus.

See original GitHub issue

Formerly “NumberBox FocusState value remains to Unfocused even after getting focus”

Describe the bug So the FocusState value seems to remain unchanged regardless of the actual focus state of the control.

Steps to reproduce the bug

1. Minimal Xaml :

<Page
    x:Class="Test.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:winui="using:Microsoft.UI.Xaml.Controls"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
    mc:Ignorable="d">

    <Grid>
        <winui:NumberBox x:Name="MyNumberBox" GotFocus="MyNumberBox_GotFocus" />
    </Grid>
</Page>

2. Code behind:

public MainPage()
{
	this.InitializeComponent();
}

private void MyNumberBox_GotFocus(object sender, RoutedEventArgs e)
{
	System.Diagnostics.Debug.WriteLine(MyNumberBox.FocusState);
}
  1. Build and Run. Now focus and unfocus the NumberBox by clicking it and clicking in blank space, do it several times and notice in the output window, even in the GotFocus event, the FocusState value is Unfocused.

Version Info WinUI Nuget Package Version :

NuGet package version:

Microsoft.UI.Xaml 2.4.2]

Windows 10 version Saw the problem?
1909 Yes
Device form factor Saw the problem?
Desktop Yes

Additional context

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
Austin-Lambcommented, Jan 5, 2021

WPF has an API that we could consider copying - UIElement.IsKeyboardFocusWithin

1reaction
StephenLPeterscommented, Jun 9, 2020

In the mean time you could call FocusManager.GetFocusedElement and then use VisualTreeHelper.GetParent to walk up the tree looking for your numberbox and if you find it then you know the internal text box has it.

Alternatively you can use the OriginalSource api on the event args and check if that is a text box, if so then it is presumably the numberbox’s template part.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Control.ContainsFocus Property (System.Windows.Forms)
You can use this property to determine whether a control or any of the controls contained within it has the input focus. To...
Read more >
How to determine which component has the program focus ...
1 Answer 1 ... Use the global Screen->ActiveControl property: Indicates which control currently has input focus on the screen. Read ActiveControl ...
Read more >
Document: hasFocus() method - Web APIs | MDN
This method can be used to determine whether the active element in a document has focus.
Read more >
Track element focus - Chrome Developers
Hover over the result to highlight the focused element in the viewport. Right-click the result and select Reveal in Elements panel to show...
Read more >
The AWT Focus Subsystem
The focus model is centralized around a single class, KeyboardFocusManager, that provides a set of APIs for client code to inquire about the...
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