I'm stuck with one very stupid problem - need to style selected row in WPF DataGrid.
I want to show a rectangle with blue border instead of just filling entire row with some color.
Any ideas how to implement this? It just must be some way to make it pretty easy.
Use CellStyle
and RowStyle
on DataGrid
. DataGridCell
and DataGridRow
both have IsSelected
property that can be used in a Trigger
to find out if they are selected.
Something like following should do the trick:
<DataGrid.CellStyle> <Style TargetType="DataGridCell"> <Style.Triggers> <Trigger Property="IsSelected" Value="True"> <Setter Property="Background" Value="White" /> <Setter Property="Foreground" Value="Black" /> </Trigger> </Style.Triggers> </Style> </DataGrid.CellStyle> <DataGrid.RowStyle> <Style TargetType="DataGridRow"> <Style.Triggers> <Trigger Property="IsSelected" Value="True"> <Setter Property="BorderBrush" Value="Blue" /> <Setter Property="BorderThickness" Value="2" /> </Trigger> </Style.Triggers> </Style> </DataGrid.RowStyle>
Just play around until you get it right.
I like this one:
<Style TargetType="{x:Type DataGridRow}"> <Setter Property="BorderBrush" Value="LightGray" /> <Setter Property="BorderThickness" Value="1" /> <Style.Triggers> <Trigger Property="IsSelected" Value="True"> <Setter Property="BorderBrush" Value="Blue" /> </Trigger> </Style.Triggers> <Style.Resources> <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" /> <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" /> </Style.Resources> </Style>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With