wpfdatagridribbonfocusmanager

How can i raise DataGrid.LostFocus on RibbonButton.Click?


The following code uses a ToolBar and correctly causes DataGrid.LostFocus (there by committing any uncomitted row-edits before the command is executed);

<Toolbar FocusManager.IsFocusScope="False">
  <Button Command="{Binding CommandName}" />
</ToolBar>
<DataGrid ... />

However, I am now trying to do the same using the Ribbon (oct10 release), but the following does not cause DataGrid.LostFocus to be raised;

<Ribbon>
  <RibbonTab>
    <RibbonGroup FocusManager.IsFocusScope="False">
      <RibbonButton Command="{Binding CommandName}" />
    </RibbonGroup>
  </RibbonTab>
</Ribbon>
<DataGrid ... />

How can I raise this event using the Ribbon? I have tried moving the IsFocusScope through the other levels (Ribbon, RibbonTab, RibbonButton) to no avail.


Solution

  • Don't like this due to the need for code behind, but in the absence of any other answers;

    <Ribbon>
      <RibbonTab>
        <RibbonGroup>
          <RibbonButton Command="{Binding CommandName}" Click="dropFocus" />
        </RibbonGroup>
      </RibbonTab>
    </Ribbon>
    <Control IsTabStop="False" Name="focusControl"/>
    <DataGrid ... />
    

    And the code behind;

    private void dropFocus(object sender, RoutedEventArgs e)
    {
      Keyboard.Focus(focusControl);
    }