Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF DataGrid - highlight Selected row even when SelectedItem is a bound property

Tags:

c#

wpf

datagrid

I've got a WPF DataGrid where the SelectedItem is bound to a ViewModel property.

SelectedItem="{Binding DataContext.SelectedBooking, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" 

If a user clicks on a Row, selecting it, the only visual clue is that the gray background of the row becomes very slightly lighter. I need to make this more obvious, so I tried adding these, individually:

<DataGrid.RowStyle>
    <Style TargetType="{x:Type DataGridRow}">
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Foreground" Value="Red"/>
                <Setter Property="Background" Value="Red" />
            </Trigger>
        </Style.Triggers>
    </Style>
</DataGrid.RowStyle>

And

<DataGrid.Resources>
    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red"/>
</DataGrid.Resources>

The result is the same. When a user single-clicks on a row it very briefly flashes red and then goes back to pale gray, although the Row actually remains as being Selected. If they click on it a second time, it goes red and stays red.

If I remove the Binding on SelectedItem, it works as expected. How can I make this work regardless of the Binding?

like image 454
Bob Tway Avatar asked Nov 20 '14 17:11

Bob Tway


1 Answers

I found the answer myself, scrolling through the Intellisense for SystemColors. There's an InactiveSelection brush that you can override too.

<DataGrid.Resources>
    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red"/>
    <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="Red"/>
</DataGrid.Resources>
like image 150
Bob Tway Avatar answered Oct 04 '22 22:10

Bob Tway