In the following simple WPF application a TextBox
is set to update a property when the focus is lost from that control, like so
<DockPanel>
<ToolBar DockPanel.Dock="Top">
<Button>Test</Button>
</ToolBar>
<TextBox Text="{Binding MyString}" />
</DockPanel>
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
public string MyString
{
get { return _myString; }
set { _myString = value; }
}
However when I run this application, enter some text in the text box and then click on the "Test" button my breakpoint on the MyString
property is not raised, also any event handler for the LostFocus
event is not raised either. These events are only raised when focus is lost from the control via some other means (e.g. the window is closed).
This is causing problems for us as in reality the "Test" button contains logic which relies on the MyString
property being updated.
How can I ensure that the LostFocus
event is correctly raised and that the binding is updated as I click on the "Test" button? It looks like the problem is somehow caused by the use of the ToolBar
, as replacing the ToolBar
with a standard button does not result in this behaviour.