Search code examples
wpfxamldatagrid

WPF Data grid Background color of selected row


I want to set background color of the selected row. Please see the below code. Foreground color (text color) is working but its not changing background color of row.

<Style TargetType="{x:Type DataGridCell}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGridCell}">
                <Grid>
                    <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" />
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="DataGridCell.IsSelected" Value="True">
            <Setter Property="Foreground" Value="Blue" />
            <Setter Property="Background" Value="Green" />
        </Trigger>
    </Style.Triggers>
</Style>


<DataGrid x:Name="Details" Grid.Column="1" Grid.Row="3" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="10,10,10,10"
          ItemsSource="{Binding Details}" >
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding Index}" Header="Index" />
        <DataGridTextColumn Binding="{Binding One}" Header="1" />
        <DataGridTextColumn Binding="{Binding two}" Header="2" />
        <DataGridTextColumn Binding="{Binding three}" Header="3" />
    </DataGrid.Columns>
</DataGrid>

Solution

  • Now issue is solved after adding specific style for row itself. It was not working when i added for cell.

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