Eisenhorn
Eisenhorn

Reputation: 1752

Change Background Color of whole datagrid row upon selection

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:

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:

Here's a screenshot of the outcome:

enter image description here

Upvotes: 2

Views: 2513

Answers (2)

Harrison
Harrison

Reputation: 3953

What if you were to remove the margin in the cell definition? That is accounting for the cells taking up extra space and the red not covering that space. If you removed the margin, do you get what you are looking for. I think the real answer may be in the DataGrid.RowBackground [Property][1].

Property Value Type: System.Windows.Media.Brush The brush that paints the background of a row. The registered default is null. For more information about what can influence the value, see DependencyProperty.

You could use a trigger on the IsSelected state to set the color. By default the entire row of a DataGrid is selected.

<DataGrid Name="dataGrid1" Margin="12,12,0,0">
    <DataGrid.RowStyle>
        <Style TargetType="DataGridRow">
            <Setter Property="Background" Value="LightBlue" />
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Background" Value="Red"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </DataGrid.RowStyle>
</DataGrid>

Upvotes: 1

MSNetDev
MSNetDev

Reputation: 141

Inorder to change default background row selection. you need to 1) edit datagridrow style & template (i.e http://msdn.microsoft.com/en-us/library/cc278066%28v=vs.95%29.aspx) 2) handle selectionchanged event and changed background of row. 3) or on datagrid row loaded event get the childrenoftype rectangle equal to "BackgroundRectangle" and set color you want - so using this it effects to all the rows in a datagrid, it is similar to 1 but doing this in code behind.

hope this gives you some idea.

Upvotes: 0

Related Questions