Brothers In Code

...a serious misallocation of .net resources

WPF ToolBar Buttons Do Not Cause Loss Of Focus

Consider this little WPF Window:

      <ToolBar DockPanel.Dock="Top" >
        <Button Content="Save" Name="BtnSave" Click="BtnSave_Click" />
        <TextBox Name="IptTextBox" Text="{Binding Content, ElementName=OptLabel}"  />
        <Label Name="OptLabel" Content="Hello" />
        <Button Content="Lose Focus" />

If you type something into the text box and then click the "Lose Focus" button, the label is updated as expected (since the default UpdateSourceTrigger on the binding is on focus).  However, if you click on the save button in the toolbar, the label is not updated.

According to MSDN, ToolBars, in addition to a couple of other objects, have a different "Logical Focus Scope." To fix this problem, I added the following code to my save button:

      Control focusedElement = FocusManager.GetFocusedElement(this) as Control;
      if (focusedElement != null)
        focusedElement.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));