MoveFocus 不会在 TabItem Header 单击事件上触发

MoveFocus won't trigger on TabItem Header click event

DataGrid 位于 Tab1。如果我位于 Tab2 并单击 Tab1 header 程序切换到 Tab1 并且 DataGrid 在正确的位置滚动到视图中,但是除非我再次单击 Tab1 header,否则选定的 Row 不会获得焦点(突出显示)。其余代码触发得很好。

CS

private void Tab1_Clicked(object sender, MouseButtonEventArgs e)
{
    if (dg_address.SelectedIndex > -1)
    {
        dg_address.ScrollIntoView(dg_address.Items[dg_address.SelectedIndex]);
        DataGridRow row = (DataGridRow)dg_address.ItemContainerGenerator.ContainerFromIndex(dg_address.SelectedIndex);
        row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
    }
}

XAML

<TabControl x:Name="tab_control"
            HorizontalAlignment="Stretch"
            VerticalAlignment="Stretch"
            Background="#FFE5E5E5">

    <TabItem>
        <TabItem.Header>
            <Label Content="Seznam"
                   MouseLeftButtonDown="Tab1_Clicked"/>
        </TabItem.Header>

在这里找到了解决方案:https://social.msdn.microsoft.com/Forums/vstudio/en-US/3baa240a-c687-449e-af77-989ff4d78333/how-to-move-focus-to-a-textbox-in-a-tabcontrol-on-a-button-click?forum=wpf

private void Tab1_Clicked(object sender, MouseButtonEventArgs e)
{
    if (dg_address.SelectedIndex > -1)
    {
        dg_address.ScrollIntoView(dg_address.Items[dg_address.SelectedIndex]);
        Dispatcher.InvokeAsync(() =>
        {
            DataGridRow row = (DataGridRow)dg_address.ItemContainerGenerator.ContainerFromIndex(dg_address.SelectedIndex);
            row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
        }
        );
    }
}

private void Tab1_Clicked(object sender, MouseButtonEventArgs e)
{
    if (dg_address.SelectedIndex > -1)
    {
        dg_address.ScrollIntoView(dg_address.Items[dg_address.SelectedIndex]);
        Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() =>
        {
            DataGridRow row = (DataGridRow)dg_address.ItemContainerGenerator.ContainerFromIndex(dg_address.SelectedIndex);
            row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
        }
        ));
    }
}

编辑:优化并删除了代码中的错误。