1
votes

How could I keep text in my row coloured for example white when DataGrid Row is Selected, and also when it is in a Inactive Selection mode (it was selected and now user clicked on another control i.e textbox).

I tried with this ( set Cell Style):

<DataGrid.CellStyle>
    <StaticResource ResourceKey="DataGridCentering"/>
</DataGrid.CellStyle>

Where I said in my App.Xaml:

<Style x:Key="DataGridCentering" TargetType="{x:Type DataGridCell}">
     <Setter Property="Template">
         <Setter.Value>
             <ControlTemplate TargetType="{x:Type DataGridCell}">
                 <Grid Background="{TemplateBinding Background}">
                     <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center"  />
                 </Grid>
              </ControlTemplate>
         </Setter.Value>
     </Setter>
     <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
     <Style.Triggers>
         <Trigger Property="IsSelected" Value="True">
             <Setter Property="Foreground" Value="White"/>
         </Trigger>
      </Style.Triggers>
</Style>

As it is possible to notice, I tried to do it with triggers, i.e when cell is selected colour my text inside cell with white colour etc, but obliviously this is not working

My text in DataGrid when cell/row is selected is still black..

2

2 Answers

1
votes

Try to add the following brush resources to the DataGrid:

<DataGrid>
    <DataGrid.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="White"/>
        <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}" Color="White"/>
    </DataGrid.Resources>
    ...
</DataGrid>

Should work on Windows 7. On Windows 8 and later you will have to override the control template of the DataGridRow.

2
votes

Try this Style

<DataGrid.Resources>
    <Style TargetType="{x:Type DataGridCell}">
        <Style.Triggers>
            <Trigger Property="IsSelected"
                     Value="True">
                <Setter Property="Foreground"
                        Value="Red" />
            </Trigger>
        </Style.Triggers>
    </Style>
</DataGrid.Resources>