WPF focus DataGrid during GotFocus event of another control 问题

WPF focus DataGrid during GotFocus event of another control problem

根据 Event for when KeyboardNavigation.TabNavigation Cycle occurs 的答案,首选解决方案是添加不可见控件作为 Detail 焦点范围的最后一个 TabIndex,处理此虚拟元素上的 GotFocus()。作为处理此 'event' 的一部分,我想将焦点移回主网格 MasterDG.Focus():

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="2*"/>
        <RowDefinition Height="1*"/>
    </Grid.RowDefinitions>
    <DataGrid Name="MasterDG" ItemsSource="{Binding Items}" FocusManager.IsFocusScope="True"/>
    <StackPanel Name="Detail" Grid.Row="1"  FocusManager.IsFocusScope="True">
        <TextBox/>
        <TextBox/>
        <TextBox/>
        <Control Name="DummyControl" 
                 GotFocus="DummyControl_GotFocus"/>
    </StackPanel>
</Grid>

事件处理程序

private void DummyControl_GotFocus(object sender, RoutedEventArgs e)
{
    Save(); //save when done editing last element of detail
    MasterDG.Focus();
}

然而,这不仅会导致 MasterDG 聚焦,还会 在当前单元格上进入编辑模式 插入 \t 字符覆盖任何单元格内容。我该如何解决这个问题?
注意 Detail 的实际内容是动态生成的。

一个简单的解决方法是在下一个调度程序周期调用 Focus()

private void DummyControl_GotFocus(object sender, RoutedEventArgs e)
{
    Save(); //save when done editing last element of detail
    Dispatcher.BeginInvoke(new Action(() => MasterDG.Focus()));
}