Selecting FullRow in DataGrid WPF

StashyCode 61 Reputation points
2021-11-09T18:41:49.747+00:00

I have a DataGrid looking like this:

<DataGrid  
       AutoGenerateColumns="False"  
       EnableColumnVirtualization="True"  
       EnableRowVirtualization="True"  
       GridLinesVisibility="Horizontal"  
       IsReadOnly="False"  
       ItemsSource="{Binding EmployeesCollectionView}"  
       KeyboardNavigation.TabNavigation="Once"  
       SelectionMode="Extended"  
       SelectionUnit="FullRow"  
       VirtualizingPanel.VirtualizationMode="Recycling">  
       //...  
</DataGrid>  

It is bound to an ObservableCollection.

The columns' style:

<Style x:Key="ColumnStyle" TargetType="DataGridCell">  
      <Setter Property="VerticalAlignment" Value="Bottom" />  
      <Setter Property="HorizontalAlignment" Value="Center" />  
      <Setter Property="Foreground" Value="Black" />  
</Style>  

This is how the row looks upon selecting it:
147840-example.png

The only way to select it is to click on any of the cells (only where there is text) and both the cells and the row get highlighted.

I want to be able to click anywhere on the row, not only where there is the text, and to not have all the cells highlighted in this (respectfully) ugly way.

I am most likely missing something very trivial, but what is it exactly?

I have tried setting HorizontalContentAlignment instead of HorizontalAlignment and VerticalContentAlignment instead of VerticalAlignment as suggested in many places and the selection looks okay, but the text is positioned in the upper left corner, which is not my intention.

Developer technologies | Windows Presentation Foundation
Developer technologies | XAML
{count} votes

Accepted answer
  1. Peter Fleischer (former MVP) 19,341 Reputation points
    2021-11-09T20:58:56.18+00:00

    Hi Stashy,
    you can use "empty" Columns to click on every point in row:

    XAML:

    <DataGrid  
        AutoGenerateColumns="False"  
        EnableColumnVirtualization="True"  
        EnableRowVirtualization="True"  
        GridLinesVisibility="Horizontal"  
        IsReadOnly="False"  
        ItemsSource="{Binding EmployeesCollectionView}"  
        KeyboardNavigation.TabNavigation="Once"  
        SelectionMode="Extended"  
        SelectionUnit="FullRow"  
        VirtualizingPanel.VirtualizationMode="Recycling">  
      <DataGrid.Columns>  
        <DataGridTextColumn Header="ID" Binding="{Binding ID}"/>  
        <DataGridTextColumn Width="100"/>  
        <DataGridTextColumn Header="Name" Binding="{Binding Name}"/>  
        <DataGridTextColumn Width="*"/>  
      </DataGrid.Columns>  
    </DataGrid>  
    

    Result:

    147828-x.gif

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.