I'm currently building up a WPF Window holding a table using the DataGrid. Binding and updates work fine, I'm also quite contempt with the styling, but I ran into troubles when it comes to selection. This are the prerequisites:
- Table is ReadOnly
- Whole Row Selection
This is the source code for my table: (Yes, I know I did set the selection color 3 times, once for DataGrid, once for the row, once for the cell. I thought maybe one of those would be helping, but it's not the case.)
<DataGrid x:Name="dgv" SelectionMode="Single" SelectionUnit="FullRow" AutoGenerateColumns="False" Grid.Column="0" Grid.RowSpan="3" Margin="8" RowHeight="32" GridLinesVisibility="Horizontal" HeadersVisibility="Column" HorizontalScrollBarVisibility="Hidden"
CanUserAddRows="False"
CanUserDeleteRows="False"
CanUserReorderColumns="False"
CanUserResizeColumns="False"
CanUserResizeRows="False"
CanUserSortColumns="True"
IsReadOnly="True"
LoadingRow="dgv_LoadingRow"
>
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red"></SolidColorBrush>
</Style.Resources>
<Setter Property="VerticalAlignment" Value="center"></Setter>
<Setter Property="Padding" Value="4"></Setter>
<Setter Property="Margin" Value="4"></Setter>
</Style>
</DataGrid.CellStyle>
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red"></SolidColorBrush>
</Style.Resources>
</Style>
</DataGrid.RowStyle>
<DataGrid.Style>
<Style TargetType="DataGrid">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red"></SolidColorBrush>
</Style.Resources>
</Style>
</DataGrid.Style>
It then continues to Column- and RowDefinitions ...
I run into the following problems:
- Only the cells get selected, not the whole line. The margins in the Cells make it look quite weird (See Screenshot)
- The row doesn't select when I click on a margin of a cell (the areas in the Screenshot that are not rendered in red) - makes selecting rows quite unintuitive...
- The cell that I click on to select the row still gets hihglighted (notice the black border around "Peter Müller" in the selected row)
Here's a screenshot of the outcome:
