How to make DataGrid a single stop as a whole on focus traversal with arrow keys row selection?
c#, datagrid, focus, tabstop, wpf
Solution
So, my suggestion to 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 have added the PreviewKeyDown event on the DataGrid, and I have removed the cell selection from each cell. As a result, it will seem like selection is on row only.
In the code behind, this is what opens the links with 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 atleast of some help.
Problem
I have a `Window` with few controls on it. One of them is a `DataGrid`. I want to implement some non-default focus traversal. Namely: - `DataGrid` is a single stop as a whole, not each row. - When `DataGrid` is focused, user can navigate through rows using up and down keys. - Navigating through columns using left and right keys is not allowed. - First column (and the only relevant for navigation) is of type `DataGridHyperlinkColumn`. When user hits Space or Enter key, it executes the hyperlink. At the moment I 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, it does not reach my expectations. Could you, please, explain how to achieve this?