Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 948
Der_Meister Avatar asked Feb 10 '17 09:02

Der_Meister


2 Answers

There is no property that you can set to change this behaviour. A row is not supposed to be clicked without a cell being click basically. This is how the controls works.

But you could easily work around this though. Just handle the MouseLeftButtonDown event of the DataGridRow and select it explicitly:

<DataGrid.RowStyle>
    <Style TargetType="{x:Type DataGridRow}">
        <EventSetter Event="MouseLeftButtonDown" Handler="DataGrid_MouseLeftButtonDown" />
        <Setter Property="Background" Value="LightGray" />
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Background" Value="Blue" />
            </Trigger>
        </Style.Triggers>
    </Style>
</DataGrid.RowStyle>

private void DataGrid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    DataGridRow dgr = sender as DataGridRow;
    dgr.IsSelected = true;
}

When a control doesn't behave the way you like or the way you expect, you either use another control, write your own from scratch or modify the behaviour of the existing one by writing some code :)

like image 97
mm8 Avatar answered Sep 29 '22 11:09

mm8


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>
like image 28
Ron Avatar answered Sep 29 '22 13:09

Ron