当我单击特定列并按 Tab 键时,它将关注列标题而不是下一列。我将如何解决它。
<controls:DataGrid x:Name="grid" CanUserSortColumns="True" SelectionMode="Extended"
Height="326" Width="1660" HorizontalAlignment="Center" VerticalAlignment="Top" HorizontalContentAlignment="Center"
AutoGenerateColumns="False" BorderBrush="Black" BorderThickness="1" FontSize="10" GridLinesVisibility="All"
ColumnHeaderHeight="25" RowBackground="White" TabFocusNavigation="Cycle" TabNavigation="Cycle"
VerticalScrollBarVisibility="Auto">
<controls:DataGrid.Columns>
<controls:DataGridTextColumn Width="20" Binding="{Binding Id, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" FontSize="10"/>
<controls:DataGridTemplateColumn Header="PG" Tag="PG" Width="44">
<controls:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox IsReadOnly="True" Text="{Binding RPe,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="45" MinHeight="28" HorizontalAlignment="Left"
Margin="8 -4 0 0" Padding="20 5 0 0" FontSize="10" Background="Transparent" BorderThickness="0" CornerRadius="0" />
</DataTemplate>
</controls:DataGridTemplateColumn.CellTemplate>
</controls:DataGridTemplateColumn>
<controls:DataGridTemplateColumn Header="P" Width="174" CanUserSort="True" Tag="Part" >
<controls:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="10,0,0,15">
<TextBlock x:Name="pnum" Text="{Binding Pnum, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" FontSize="10"
VerticalAlignment="Center" Foreground="Black">
</TextBlock>
</StackPanel>
</DataTemplate>
</controls:DataGridTemplateColumn.CellTemplate>
</controls:DataGridTemplateColumn>
您可以捕获 PreviewKeyDown 事件并应用您的自定义行为。
<controls:DataGrid PreviewKeyDown="DataGrid_PreviewKeyDown" ... />
private void DataGrid_PreviewKeyDown(object sender, KeyRoutedEventArgs e)
{
if (sender is not DataGrid dataGrid ||
e.Key is not VirtualKey.Tab)
{
return;
}
int columnIndex = dataGrid.Columns.IndexOf(dataGrid.CurrentColumn);
columnIndex += 1;
if (columnIndex >= dataGrid.Columns.Count)
{
columnIndex = 0;
}
if (dataGrid.Columns.ElementAtOrDefault(columnIndex) is not { } column)
{
return;
}
dataGrid.CurrentColumn = column;
e.Handled = true;
}