DataGrid
位于Tab1
上。如果我位于Tab2
上,然后单击Tab1
标头,程序将切换到Tab1
,并且DataGrid
会滚动到正确位置的视图中,但是所选的{{ 1}}不会变得聚焦(突出显示),除非我再次单击Row
标头。其余代码都可以正常触发。
CS
Tab1
XAML
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));
}
}
答案 0 :(得分:1)
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));
}
));
}
}
编辑:优化并删除了代码中的错误。