---
title: "The difference between GotFocus and GotKeyboardFocus in wpf?"  
description: "The difference between GotFocus and GotKeyboardFocus in wpf?"  
author: "Anonymous User"  
published: 2013-08-10  
updated: 2013-08-10  
canonical: https://www.mindstick.com/forum/1366/the-difference-between-gotfocus-and-gotkeyboardfocus-in-wpf  
category: "wpf"  
tags: ["wpf"]  
reading_time: 2 minutes  

---

# The difference between GotFocus and GotKeyboardFocus in wpf?

What is the [difference](https://www.mindstick.com/articles/157114/good-news-or-bad-news-and-the-difference-is)(s) between GotFocus and GotKeyboardFocus -and similarly LostFocus and LostKeyboardFocus?\
\

I am creating a [custom](https://www.mindstick.com/blog/12298/use-custom-writing-services-to-get-through-the-finals) [control](https://www.mindstick.com/blog/197/asp-dot-net-repeater-control) by extending Control class. Something like [ComboBox](https://www.mindstick.com/articles/57/display-record-according-to-combobox-selection-in-csharp-dot-net) but with some other effects. I'm trying to open and close a Popup by setting a [property](https://www.mindstick.com/blog/205/property-notification-in-c-sharp): IsDropDownOpen just like a ComboBox through the GotFocus and LostFocus events. I don't want to Popup get closed, when I Alt+Tabed the [windows](https://www.mindstick.com/articles/311752/how-to-install-and-use-the-google-wifi-software-on-a-windows-or-mac-computer), but get closed when I click on a [Button](https://www.mindstick.com/articles/63/how-to-add-button-in-datagridview-in-csharp-dot-net) for example or I go to a TextBox. I did:

[private](https://www.mindstick.com/blog/11097/java-access-modifiers-the-public-and-the-private-modifiers) [static void](https://www.mindstick.com/interview/2715/can-we-write-static-public-void-instead-of-public-static-void) OnGotFocusHandler(object sender, RoutedEventArgs e) {

if (e.Handled)

return;

((SearchBox)sender).IsDropDownOpen = true;

e.Handled = true;

}

private static void OnLostFocusHandler(object sender, RoutedEventArgs e) {

if (e.Handled)

return;

((SearchBox)sender).IsDropDownOpen = false;

e.Handled = true;

}

The GotFocus works. But the Lost one didn't. If I do the Lost stuff in LostKeyboardFocus then when I Alt+Tab the windows, or Window goes to inactive, then the method get called, while I don't want. How can I solve it?

## Replies

### Reply by Pravesh Singh

WPF has 2 concepts regarding focus. There is the physical keyboard focus, and there is logical focus. Only one element can have keyboard focus (and if the application isn't the active application, no element will have keyboard focus).

Multiple items can have logical focus. In fact, you can create new "focus scopes". As per MSDN:

When keyboard focus leaves a focus scope, the focused element will lose keyboard focus but will retain logical focus. When keyboard focus returns to the focus scope, the focused element will obtain keyboard focus. This allows for keyboard focus to be changed between multiple focus scopes but ensures that the focused element in the focus scope regains keyboard focus when focus returns to the focus scope.

You can define your own focus scope on an element (typically a `Panel`) by setting `FocusManager.IsFocusScope="True"`. The controls in WPF that are focus scopes by default are `Window`, `MenuItem`, `ToolBar`, and `ContextMenu`.

This makes sense if you think about having multiple `Window`s in your application. When you `Alt-Tab` between them, you expect your keyboard focus to return to the same place it was the last time the `Window` had focus. By keeping keyboard focus and logical focus separate, you can achieve this.


---

Original Source: https://www.mindstick.com/forum/1366/the-difference-between-gotfocus-and-gotkeyboardfocus-in-wpf

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
