Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF DataGrid selected row style

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.

like image 225
illegal-immigrant Avatar asked Dec 27 '10 15:12

illegal-immigrant


2 Answers

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.

like image 141
decyclone Avatar answered Oct 04 '22 13:10

decyclone


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> 
like image 31
Jeson Martajaya Avatar answered Oct 04 '22 15:10

Jeson Martajaya