1

I've stumbled across a nuisance in WPF that is apparently well-known: the TextBox control sometimes doesn't lose focus (and thus doesn't fire its LostFocus event). For instance, if you have the focus in a text box and you click a button, the text box doesn't lose focus from a programmatic standpoint.

A lot of questions and answers address this, for instance this one and this one. There are also a lot of hackish ways out there for dealing with this, but the ones I've seen have all revolved around persisting recently-modified data in a textbox so as not to lose recent changes unintentionally. They accomplish this by forcing an update to the source when the "save" command occurs.

I have a different problem, though: I'm trying to update other controls in my UI based on the value in a textbox. So, a user enters a value in the textbox, and when they leave the textbox, it should result in computed values being rendered in other controls. This doesn't happen consistently, though, when the textbox doesn't consistently fire the LostFocus event.

None of the sources I've found have said why the TextBox behaves this way, though they all come across as if this is normal behavior. For example, the first sentence of this answer:

The problem is that the TextBox does, in fact, not lose focus when the menu item is activated.

That's it. The author moves on as if that's normal.

Why does it behave this way, or more specifically, if it's intentional, then what is its purpose? And how can I overcome it in my case where I want other controls to change based on newly-entered data in a textbox?

Community
  • 1
  • 1
rory.ap
  • 34,009
  • 10
  • 83
  • 174
  • It is entirely normal and all other GUI class libraries work the same way. If it didn't work that way then, say, an Edit + Copy or Edit + Undo menu command would be quite hard to implement. – Hans Passant Sep 16 '16 at 22:24
  • I'm not sure I follow, but in my experience (using WinForms, and now starting with WPF), a UI can only have one focused control at a time. So if you enter text in a textbox, and then click a button, now the focus is on the button (and the proper LostFocus event has fired on the textbox). – rory.ap Sep 16 '16 at 22:53
  • `TextBox` will loose focus only if another control **in the same focus scope** gains focus (this will result in the caret disappearing from the `TextBox`). So clicking a `Button` will result in `TextBox` losing focus only if it is in the same focus scope **and** it is capable of gaining focus (in particular its `Focusable` property reads `true`) **and** does so upon clicking. – Grx70 Sep 18 '16 at 07:56

1 Answers1

1

The reason is that focus is more complex than it appears. If the focus in a text box and you alt-tab to another application, you don't really want your validation to fire. Similarly if you select some text and then pull down the edit menu and select 'copy', you don't want to lose the focus on the selected text.

LostFocus only fires when you move to another control that accept focus in the same application.

LostKeyboardFocus also fires when you move to another application, or a menu.

Robin Bennett
  • 3,192
  • 1
  • 8
  • 18