问题
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?
来源:https://stackoverflow.com/questions/39539053/what-is-the-reason-why-textbox-does-not-lose-focus-in-certain-cases-and-how-can