0
votes

I have a WPF DataGrid where the row changes color when a checkbox in the row is selected. I have a problem where if a row is highlighted (such as being clicked on by the mouse) and then the DataGrid loses focus then the row loses it's custom color.

Here is were the custom color is set

<Style x:Key="DataGridStyle" TargetType="{x:Type DataGrid}">
    <Setter Property="RowStyle">
        <Setter.Value>
            <Style TargetType="DataGridRow">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding IsSelected}" Value="True">
                        <Setter Property="Background" Value="#CBE5F2"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Setter.Value>
    </Setter>
</Style>

Pictures of the problem:

Row is highlighted with mouse: Row Highlighted

After DataGrid loses focus Datagrid Lost Focus

Question: Is there a way to have the highlighted row keep it's custom color after the DataGrid loses focus.

1

1 Answers

0
votes

I found a work around which is to set the cell style instead of the row style:

   <Setter Property="CellStyle">
        <Setter.Value>
            <Style TargetType="DataGridCell">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding IsSelected}" Value="True">
                        <Setter Property="Background" Value="#CBE5F2"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Setter.Value>
    </Setter>

It overrides the selected highlighting (bright blue) completely, so it's not ideal, but it works even after losing focus.