Provide an API for determining if the "main" component of a control has focus.
See original GitHub issueFormerly “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);
}
- 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 isUnfocused
.
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:
- Created 3 years ago
- Comments:7 (6 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
WPF has an API that we could consider copying - UIElement.IsKeyboardFocusWithin
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.