Search code examples
wpfdatagridrowselectioncell

DataGrid Cell (or row) selection works only when clicking on the content (the text) of the cell(s)


I'm trying to obtain a DataGrid in WPF with certain characteristics, but I'm having an Issue. I've made the columns so that every column has the same width (Width="*"), and they fill the whole space available. That works great, but now there is a problem: as I try to select a row (that's what I need to do) by clicking on a blank spot (somewhere where there isn't any text basically), the row or the cell doesn't get selected.

Let me post my code (ignore the Bindings which work perfectly):

<DataGrid
                      ItemsSource="{Binding MyBinding}" 
                      SelectedItem="{Binding Selected}"
                      AutoGenerateColumns="False" 
                      IsReadOnly="True"
                      IsManipulationEnabled="False"
                      CanUserResizeColumns="False"
                      CanUserResizeRows="False"
                      GridLinesVisibility="Horizontal"
                      SelectionMode="Single"
                      SelectionUnit="FullRow"
                      RowHeaderWidth="0"
                      Height="Auto"
                      SelectedIndex="0"
                      >
                <DataGrid.Columns>
                    <DataGridTextColumn Header="Param1" Binding="{Binding Param1}" Width="*" />
                    <DataGridTextColumn Header="Param2" Binding="{Binding Param2}" Width="*"/>
                     ...
                    <DataGridTextColumn Header="Paramx" Binding="{Binding Paramx}" Width="*"/>
                </DataGrid.Columns>
                <DataGrid.CellStyle>
                    <Style TargetType="DataGridCell">
                        <Setter Property="BorderThickness" Value="0"/>
                        <Setter Property="FrameworkElement.HorizontalAlignment" Value="Center"/>
                    </Style>
                </DataGrid.CellStyle>
                <DataGrid.ColumnHeaderStyle>
                    <Style TargetType="DataGridColumnHeader">
                        <Setter Property="HorizontalContentAlignment" Value="Center"/>
                    </Style>
                </DataGrid.ColumnHeaderStyle>


            </DataGrid>

https://i.sstatic.net/c9wyp.png

As you can see the selection only affects the content of the cell and not the cell itself. I want it to work in any spot of the row.

Thank you in advance for your help!

EDIT: It seems to work as I set the HorizontalAlignment to Stretch. However I still want it centered.

EDIT2: Solved by doing that:

<DataGrid.CellStyle>
                    <Style TargetType="{x:Type DataGridCell}">
                        <Setter Property="BorderThickness" Value="0"/>
                        <Setter Property="FrameworkElement.HorizontalAlignment" Value="Stretch"/>
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type DataGridCell}">
                                    <Grid Background="{TemplateBinding Background}">
                                        <ContentPresenter HorizontalAlignment="Center" />
                                    </Grid>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </DataGrid.CellStyle>

Solution

  • You really want to set the TextAlignment property to center. But unfortunately you don't have access to that property from the FrameworkElement.

    You're probably better off using a DataGridTemplateColumn to get the desired effect.