How do I find out where the focus is going in my WPF application?
Asked Answered
C

2

8

I have a search screen in my WPF application. The screen is implemented as a UserControl in a TabItem of a TabControl. When the user switches to the Search tab, I want the focus to go into one particular field.

So I added a Loaded event handler to the UserControl tag in the Xaml and I called the Focus method of the control I want to have the initial focus in the Loaded event handler. This worked great until I upgraded the Telerik control library I'm using today. Now, when I switch to the Search tab, the focus is NOT in the field I want to have it, but I can't tell what control does have the focus.

The field I want to have focus already has GotFocus & LostFocus event handlers for other reasons. I remembered that in Win Forms, the LostFocus event handler arguments tell you which control is going to get the focus. So I put a breakpoint in my LostFocus handler & discovered that the arguments to the LostFocus event handler in WPF don't include that information.

How can I figure out where the focus is going without putting GotFocus handlers on every control in my UserControl?

Tony

Cardoso answered 30/3, 2012 at 0:12 Comment(0)
H
9

You can try putting your breakpoint on the LostKeyboardFocus Attached Event instead of the LostFocus Event. It uses the KeyboardFocusChangedEventArgs Class which does have properties that show which element had focus and where the focus is going.

private void textBox1_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
    textBox1.Text = ((FrameworkElement)e.NewFocus).Name ; 
}
Hypothermia answered 30/3, 2012 at 4:21 Comment(1)
Thanks, this is what I needed. I couldn't tell easily where the focus was going because it went to a checkbox I have on the form, and this shows focus by putting a dashed box around the inside of the check box. It's tiny & hard to see unless you're really looking for it. At least, it is in my application.Cardoso
S
10

Try to press Tab Key and see if it helps you find the control in focus.

You can also use Snoop as suggested in this Q/A: Any tips on debugging focus issues in WPF?

For starters, Snoop shows the current focused element and the current FocusScope in the status bar.

You can get it to show you all the GotFocus and LostFocus events:

1. Run your app.
2. Run Snoop.
3. Choose your app in the dropdown.
4. Click the binoculars ("Snoop") button.
5. On the right pane, click the Events tab.
6. Click to bring down the dropdown.
7. Scroll down to the Keyboard section and check GotKeyboardFocus, LostKeyboardFocus, and optionally the PreviewXXX events.
8. Now do what you need to do to manipulate focus and watch the Snoop window.

Similarly you can track the FocusManager events the same way.

Seema answered 30/3, 2012 at 1:8 Comment(1)
Thanks, I'll give it a shot in the morning.Cardoso
H
9

You can try putting your breakpoint on the LostKeyboardFocus Attached Event instead of the LostFocus Event. It uses the KeyboardFocusChangedEventArgs Class which does have properties that show which element had focus and where the focus is going.

private void textBox1_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
    textBox1.Text = ((FrameworkElement)e.NewFocus).Name ; 
}
Hypothermia answered 30/3, 2012 at 4:21 Comment(1)
Thanks, this is what I needed. I couldn't tell easily where the focus was going because it went to a checkbox I have on the form, and this shows focus by putting a dashed box around the inside of the check box. It's tiny & hard to see unless you're really looking for it. At least, it is in my application.Cardoso

© 2022 - 2024 — McMap. All rights reserved.