Search code examples
wpfwpfdatagrid

Click on row in DataGrid does not select it


enter image description here

Black background - cell. Gray background - row. Blue background - selected row.

If I click on row, it does not become selected. However, if I click on cell, row is selected correctly.

<Window x:Class="Test021000.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DataGrid Width="200" Height="200" ItemsSource="{Binding}" AutoGenerateColumns="False" SelectionUnit="FullRow">
            <DataGrid.DataContext>
                <x:Array Type="{x:Type sys:String}">
                    <sys:String>1</sys:String>
                    <sys:String>2</sys:String>
                    <sys:String>3</sys:String>
                    <sys:String>4</sys:String>
                    <sys:String>5</sys:String>
                </x:Array>
            </DataGrid.DataContext>
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding}" Width="100">
                    <DataGridTextColumn.CellStyle>
                        <Style TargetType="{x:Type DataGridCell}">
                            <Setter Property="Background" Value="Black" />
                            <Setter Property="Foreground" Value="White" />
                            <Setter Property="Margin" Value="15" />
                        </Style>
                    </DataGridTextColumn.CellStyle>
                </DataGridTextColumn>
            </DataGrid.Columns>
            <DataGrid.RowStyle>
                <Style TargetType="{x:Type DataGridRow}">
                    <Setter Property="Background" Value="LightGray" />
                    <Style.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="Background" Value="Blue" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </DataGrid.RowStyle>
        </DataGrid>
    </Grid>
</Window>

Solution

  • I think this is related to the template of the DataGridCell. I would suggest using DataGridTemplateColumn, in which the Margin is not set of the Cell:

    <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding}" Width="100">
                    <DataGridTextColumn.CellStyle>
                        <Style TargetType="{x:Type DataGridCell}">
                            <Setter Property="Background" Value="Black" />
                            <Setter Property="Foreground" Value="White" />
                            <Setter Property="Margin" Value="15" />
                        </Style>
                    </DataGridTextColumn.CellStyle> 
                </DataGridTextColumn>
                <DataGridTemplateColumn Width="100">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Margin="15" Text="{Binding}" Background="Black" Foreground="White" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>