WPF DataGrid - highlight selected row even if SelectedItem is a related property

I have a WPF DataGrid where the SelectedItem is bound to the ViewModel property.

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

If the user clicks on a line, selecting it, the only visual key is that the gray background of the line becomes a little lighter. I need to make this more obvious, so I tried to add them separately:

 <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 makes a single click on a line, it flashes red very briefly, and then returns to pale gray, although the line really remains selected. If they click on it a second time, it will turn red and remain red.

If I unlink the selected item, it works as expected. How can I do this work regardless of binding?

+6
source share
1 answer

I myself found the answer, scrolling through Intellisense for SystemColors. There is an InactiveSelection brush there, which you can also override.

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

Source: https://habr.com/ru/post/978472/


All Articles