Get currently focused element/control in a WPF window
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In WPF, "focused control" usually means the element currently receiving keyboard input. That sounds simple, but WPF actually tracks both keyboard focus and logical focus, and the answer changes depending on which one you need. Understanding the distinction helps you avoid null checks, wrong casts, and event handlers that seem to fire at the wrong time.
Keyboard Focus vs Logical Focus
The fastest way to ask WPF which element currently has keyboard focus is Keyboard.FocusedElement. This returns an IInputElement, so you often cast it to FrameworkElement or a specific control type before using it.
Keyboard focus is global for the application. Only one element can have it at a time, and that element is the one that receives key presses. Logical focus is stored per focus scope. A Window, Menu, or ToolBar can remember which child last had focus even when keyboard focus moves elsewhere.
That distinction matters when a dialog opens, a popup steals focus, or a control lives inside a nested scope. If your code only cares about "what the user is typing into right now", keyboard focus is usually correct. If you need to restore focus later, logical focus is often the better tool.
Reading the Focused Element
Here is a simple window that inspects the currently focused control when a button is clicked:
This approach works well when you only need to inspect focus on demand. Notice the cast to FrameworkElement. Keyboard.FocusedElement can be null, and even when it is not null it may not be the exact control type you expect.
Tracking Focus Changes Reliably
Polling Keyboard.FocusedElement every few seconds is usually the wrong design. WPF already raises routed events for focus changes, so you can react when focus actually moves.
Using AddHandler with handledEventsToo set to true lets the window observe focus changes even when child controls mark events as handled. That makes it a solid choice for diagnostics, form navigation, and accessibility tooling.
Working with Logical Focus
If you need the remembered focused element inside a specific scope, use FocusManager.GetFocusedElement. This is useful when a region of the interface temporarily loses keyboard focus but you want to restore the previous control.
For example, a search panel can remember the last text box the user was editing. When the panel becomes active again, you can send focus back to that control instead of guessing.
Common Pitfalls
One common mistake is assuming the focused element always belongs to the current window. If a popup, menu, or another top-level window is active, Keyboard.FocusedElement may point somewhere unexpected or be null.
Another issue is casting directly to a control type such as TextBox without checking. Focus may be on a Button, ComboBoxItem, or even a template-generated element rather than the visible parent control.
Developers also mix up logical and keyboard focus. If code restores focus based on Keyboard.FocusedElement, it may fail after modal interactions because keyboard focus has already moved away. In those cases, store logical focus or track the last meaningful control explicitly.
Finally, querying focus too early is a common source of confusion. During construction, layout may not be complete yet. If initial focus matters, set it after the window has loaded.
Summary
- Use
Keyboard.FocusedElementwhen you need the control currently receiving keyboard input. - Use
FocusManager.GetFocusedElementwhen you need the remembered element inside a focus scope. - Cast carefully because the returned value is usually an
IInputElement, not a specific control. - Prefer focus events over polling when you need to react to changes.
- Initialize or restore focus after loading so WPF has a valid visual tree to work with.
Related reading
- Get DateTime as UTC with Dapper
- Get Enum from Description attribute
- Get log4net log file in C
- Get number of digits in an unsigned long integer c
- Get output parameter value in ADO.NET
- Get sum of two columns in one LINQ query
- Get the .NET assembly's AssemblyInformationalVersion value?
- Get the previous month's first and last day dates in c

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.