WPF DataGrid selected row style

illegal-immigrant picture illegal-immigrant · Dec 27, 2010 · Viewed 83.1k times · Source

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.

Answer

decyclone picture decyclone · Dec 27, 2010

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.