5

How can I get a current focused control in WPF?

I found some solution for WinForms, but invoking WIN32 API function, didn't work in WPF?

Is there any way for doing it in WPF?

rae1
  • 6,066
  • 4
  • 27
  • 48
Igal
  • 4,603
  • 14
  • 41
  • 66

3 Answers3

7

I know this is a late answer, but maybe people searching can find this helpful, I it found on msdn in the "Navigating Focus Programmatically" section close to the bottom of the page:

UIElement elementWithFocus = Keyboard.FocusedElement as UIElement;
James Esh
  • 2,219
  • 1
  • 24
  • 41
6

Here's what I did

protected override void OnPreviewLostKeyboardFocus(KeyboardFocusChangedEventArgs e)
{
    lostFocusControl = e.OldFocus;
}

private void PauseButton_PreviewKeyDown(object sender, KeyEventArgs e)
{
    // invoke OnPreviewLostKeyboardFocus handler
}
g t
  • 7,287
  • 7
  • 50
  • 85
Igal
  • 4,603
  • 14
  • 41
  • 66
0

Another solution:

bool FocusedElement = FocusManager.GetFocusedElement(this);

Found here: https://stackoverflow.com/a/8077580/14648642

Tom667
  • 95
  • 1
  • 10