How to make a DataGrid a single stop as a whole when bypassing the focus with selecting the row with arrows?

I have a Window with several controls. One of them is DataGrid . I want to implement some non-phonic focus. Namely:

  • DataGrid is a single stop in general, not every row.
  • When focusing the DataGrid user can move through the rows using the up and down keys.
  • Column navigation using the left and right keys is not allowed.
  • The first column (and only for navigation) is of type DataGridHyperlinkColumn . When the user presses the space bar or enter key, he performs a hyperlink.

I currently have the following code:

 <DataGrid x:Name="DocumentTemplatesGrid" Grid.Row="2" ItemsSource="{Binding Source={StaticResource DocumentTemplatesView}}" IsReadOnly="True" AutoGenerateColumns="False" SelectionUnit="FullRow" SelectionMode="Single" TabIndex="1" IsTabStop="True"> <DataGrid.CellStyle> <Style TargetType="DataGridCell"> <Setter Property="IsTabStop" Value="False"/> </Style> </DataGrid.CellStyle> <DataGrid.RowStyle> <Style TargetType="DataGridRow"> <Setter Property="IsTabStop" Value="False"/> </Style> </DataGrid.RowStyle> <DataGrid.Columns> <DataGridHyperlinkColumn Header="Name" Width="2*" Binding="{Binding Name}"/> <DataGridTextColumn Header="Description" Width="5*" Binding="{Binding Description}"/> <DataGridTextColumn Header="Type" Width="*" Binding="{Binding Type}"/> </DataGrid.Columns> </DataGrid> 

Unfortunately, this does not reach my expectations. Could you explain how to achieve this?

+5
source share
1 answer

So my suggestion for you is this:

  <DataGrid x:Name="DocumentTemplatesGrid" Grid.Row="2" ItemsSource="{Binding Items}" IsReadOnly="True" AutoGenerateColumns="False" SelectionMode="Single" SelectionUnit="FullRow" TabIndex="1" IsTabStop="True" PreviewKeyDown="DocumentTemplatesGrid_PreviewKeyDown"> <DataGrid.CellStyle> <Style TargetType="DataGridCell"> <Setter Property="IsTabStop" Value="False"/> <Setter Property="BorderThickness" Value="0"/> <Setter Property="FocusVisualStyle" Value="{x:Null}"/> </Style> </DataGrid.CellStyle> <DataGrid.RowStyle> <Style TargetType="DataGridRow"> <Setter Property="IsTabStop" Value="False"/> </Style> </DataGrid.RowStyle> 

I added the PreviewKeyDown event to the DataGrid, and I removed the cell selection from each cell. As a result, it will appear that the selection is performed only in a row.

In the code behind, it opens links using Space / Enter:

 private void DocumentTemplatesGrid_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e) { if (e.Key == System.Windows.Input.Key.Space || e.Key == System.Windows.Input.Key.Enter) { if (e.Source is DataGrid) { string navigationUri = ((e.Source as DataGrid).SelectedItem as Class).Name; Process.Start(navigationUri); } e.Handled = true; } } 

Hope this is what you are looking for, or at least some help.

+3
source

Source: https://habr.com/ru/post/1258985/


All Articles