The problem is that the toolbar places your button in a different FocusManager.FocusScope
. That means that both the Button
and the TextBox
can receive logical focus at the same time, each in its own scope. This is normally a good thing, since you usually don't want to lose focus in your main window area when you select menu items and ToolBar buttons, but in your case it is preventing what you are doing from working.
Although you could override the FocusManager.IsFocusScope
property on the toolbar and get the effect you want, this is probably not the best plan since it would make all the other toolbar buttons also steal focus from your main window area.
Instead you could use one of several easy solutions:
- Put your button outside the Toolbar
- Add a Focusable="true" control to your main window area and focus it when the button is clicked
- Manually force the update by calling
textBox.GetBindingExpression(TextBox.TextProperty).UpdateSource()
- Temporarily set Focusable="true" on a control in the main window, set focus to it, then immediately set Focusable="false" again
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…