Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the focused control in WPF?

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?

like image 440
Igal Avatar asked Mar 15 '11 19:03

Igal


2 Answers

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;
like image 170
James Esh Avatar answered Sep 25 '22 01:09

James Esh


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
}
like image 21
Igal Avatar answered Sep 23 '22 01:09

Igal