选项卡关注整个DataGrid,而不是DataGrid中的任何项目(将使用箭头/页面按钮在DataGrid中导航)



我有一个带有DataGrid和几个按钮的页面。我已经为FullRow设置了DataGrid的SelectionUnit。

<Button Grid.Row="0" Height="20" Width="50" Content="Config" FontSize="11"               
Background="{StaticResource DefButtonBackgroundBrush}"/>
<DataGrid Grid.Row="1"
AutoGenerateColumns="False"
IsReadOnly="True"
SelectionMode="Single"
SelectionUnit="FullRow"
GridLinesVisibility="None"
Name="MatipBatapConfigList"               
ColumnHeaderStyle="{StaticResource DGHdr_2LineNormal}"
ItemsSource="{Binding}">
...........................
</DataGrid>
<Button Grid.Row="2" Height="20" Width="50" Content="Edit" FontSize="11"               
Background="{StaticResource DefButtonBackgroundBrush}"/>
<Button Grid.Row="3" Height="20" Width="50" Content="Add" FontSize="11"               
Background="{StaticResource DefButtonBackgroundBrush}"/>
<Button Grid.Row="4" Height="20" Width="50" Content="Delete" FontSize="11"               
Background="{StaticResource DefButtonBackgroundBrush}"/>

如果我开始选项卡,焦点的顺序是

  • 配置按钮
  • DataGrid的第0列标头
  • DataGrid的第1列标题
  • DataGrid的最后一列标题
  • DataGrid第0行第0列的单元格
  • DataGrid第0行第1列的单元格
  • DataGrid最后一列第0行的单元格
  • DataGrid最后一行最后一列的单元格
  • "编辑"按钮
  • "添加"按钮
  • "删除"按钮
  • 从上到下

我想要的订单是:

  • 配置按钮
  • DataGrid(此处一站,本身没有焦点,但使用箭头按钮选择行(
  • "编辑"按钮
  • "添加"按钮
  • "删除"按钮
  • 从上到下

因此,选项卡只带您进出DataGrid,而不再带您进出。箭头/页面按钮用于在DataGrid中移动。

在过去的两天里,我尝试了很多事情,包括在标题样式、单元格样式上将IsTabStop设置为False,以及使用KeyboardNavigationMode.TabNavigation

有什么想法吗?

===============编辑:mm8的答案展开===============

我最后用了mm8的答案。这是我基于这个答案的扩展解决方案。选项卡现在只带您进入/离开DataGrid,箭头在DataGrid中逐行移动。

xaml:

<Button Grid.Row="0" Height="20" Width="50" Content="Config" FontSize="11"               
x:Name="ConfigButton"
Background="{StaticResource DefButtonBackgroundBrush}"/>
<DataGrid Grid.Row="1"
AutoGenerateColumns="False"
IsReadOnly="True"
SelectionMode="Single"
SelectionUnit="FullRow"
GridLinesVisibility="None"
Name="MatipBatapConfigList"               
PreviewKeyDown="DataGrid_PreviewKeyDown"
GotFocus="MatBatConfigList_GotFocus"
KeyboardNavigation.TabNavigation ="Once"
ColumnHeaderStyle="{StaticResource DGHdr_2LineNormal}">
...........................
</DataGrid>
<Button Grid.Row="2" Height="20" Width="50" Content="Edit" FontSize="11"               
x:Name="EditButton"
Background="{StaticResource DefButtonBackgroundBrush}"/>
<Button Grid.Row="3" Height="20" Width="50" Content="Add" FontSize="11"               
Background="{StaticResource DefButtonBackgroundBrush}"/>
<Button Grid.Row="4" Height="20" Width="50" Content="Delete" FontSize="11"               
Background="{StaticResource DefButtonBackgroundBrush}"/>

代码背后:

private void DataGrid_PreviewKeyDown(object sender, KeyEventArgs e)
{
DataGrid dg = (DataGrid)MatipBatapConfigList;
int count = dg.Items.Count;
if ((dg.SelectedIndex < 0) && (count > 0)) dg.SelectedIndex = 0;
switch (e.Key)
{
case Key.Tab:
if ((Keyboard.Modifiers & (ModifierKeys.Shift)) == ModifierKeys.Shift)
{
e.Handled = true;
ConfigButton.Focus();
}
else
{
e.Handled = true;
EditButton.Focus();
}
break;
case Key.Down:
if (count > 0)
{
if (dg.SelectedIndex == (count - 1))
{
dg.SelectedIndex = 0;
}
else
{
dg.SelectedIndex++;
}
dg.CurrentItem  = dg.Items[dg.SelectedIndex];
dg.SelectedItem = dg.Items[dg.SelectedIndex];
dg.UpdateLayout();
dg.ScrollIntoView(dg.SelectedItem);
}
e.Handled = true;
break;
case Key.Up:
if (count > 0)
{
if (dg.SelectedIndex == 0)
{
dg.SelectedIndex = count - 1;
}
else
{
dg.SelectedIndex--;
}
dg.CurrentItem  = dg.Items[dg.SelectedIndex];
dg.SelectedItem = dg.Items[dg.SelectedIndex];
dg.UpdateLayout();
dg.ScrollIntoView(dg.SelectedItem);
}
e.Handled = true;
break;
}
}

private void MatBatConfigList_GotFocus(object sender, RoutedEventArgs e)
{
DataGrid dg = (DataGrid)MatipBatapConfigList;
int count = dg.Items.Count;
if ((dg.SelectedIndex < 0) && (count > 0))
{
dg.SelectedIndex = 0;
dg.CurrentItem   = dg.Items[dg.SelectedIndex];
dg.SelectedItem  = dg.Items[dg.SelectedIndex];
if (dg.CurrentColumn == null) dg.CurrentColumn = dg.ColumnFromDisplayIndex(1);
dg.ScrollIntoView(dg.SelectedItem);
}
}

处理此问题的最简单方法可能是在XAML标记中给编辑按钮一个x:Name,并通过处理DataGrid:的PreviewKeyDown事件以编程方式集中它

private void DataGrid_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Tab)
{
e.Handled = true;
edit.Focus();
}
}

XAML:

<Button x:Name="edit" Grid.Row="2" Height="20" Width="50" Content="Edit" FontSize="11"               
Background="{StaticResource DefButtonBackgroundBrush}"/>

如果在标头中定义自定义元素,则应将这些元素的IsTabStop属性设置为false

最新更新