question

StashyCode-7539 avatar image
0 Votes"
StashyCode-7539 asked HuiLiu-MSFT commented

Selecting FullRow in DataGrid WPF

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.

windows-wpfdotnet-wpf-xaml
example.png (1.9 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi,@StashyCode-7539. I think PeterFleischer-3316’s solution is good. Are there any updates to the question?

0 Votes 0 ·

1 Answer

PeterFleischer-3316 avatar image
1 Vote"
PeterFleischer-3316 answered PeterFleischer-3316 commented

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



x.gif (121.5 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi Peter, thank you for your input. Unfortunately, I don't have space for any more columns, as I have more than 5 and they're taking up enough space. Also, won't the last column with Width="*" disable the vertical scroll viewer?

0 Votes 0 ·

Hi,
the last column with Width="*" don't disable the vertical scroll viewer.

0 Votes 0 ·