Search code examples
c#datagridwpf-controls

c#: Datagrid: Changing Foreground Color of Selection by DataTriggers


I want to costumize the colors of a Datagrid.

My code is as follows

<Window x:Class="DataGridTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:DataGridTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <DataGrid Grid.Row="1"
                  VerticalAlignment="Stretch" 
                  VerticalContentAlignment="Top"
                  GridLinesVisibility="Horizontal" 
                  VerticalScrollBarVisibility="Visible"
                  AutoGenerateColumns="True"
                  SelectionUnit="FullRow"
                      >

            <DataGrid.RowStyle>

                <Style TargetType="DataGridRow">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Path=Automatic}" Value="True">
                            <Setter Property="FontStyle" Value="Italic"/>
                            <Setter Property="Foreground"  Value="Green" />
                            <Setter Property="Background" Value="GhostWhite"/>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding Path=Automatic}" Value="False">
                            <Setter Property="Background" Value="FloralWhite"/>
                            <Setter Property="Foreground"  Value="Blue" />
                        </DataTrigger>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="Foreground"  Value="Black" /> <!-- Why is this ignored -->
                            <Setter Property="Background"  Value="Red" />
                        </Trigger>
                    </Style.Triggers>
                </Style >
            </DataGrid.RowStyle>

            <DataGrid.Columns>
                <DataGridTextColumn Header="Subject" Binding="{Binding Path=Subject}"/>
                <DataGridTextColumn Header="Body" Binding="{Binding Path=Body}"/>
                <DataGridTextColumn Header="Automatic" Binding="{Binding Path=Automatic}"/>
            </DataGrid.Columns>

            <local:Dummy Subject="Subject 1" Body="Body 1" Automatic="True" />
            <local:Dummy Subject="Subject 2" Body="Body 2" Automatic="False"  />
            <local:Dummy Subject="Subject 3" Body="Body 2"  Automatic="False" />
        </DataGrid>
    </Grid>
</Window>

and the c#

using System.Windows;
namespace DataGridTest
{
    public class Dummy
    {
        public string Subject { get; set; }
        public string Body { get; set; }
        public bool Automatic { get; set; }
    }

    public partial class MainWindow : Window
    {
        public MainWindow()
        {      
            InitializeComponent();
        }
    }
}

The result looks like (Last Line is selected):

enter image description here

What I was expecting: the whole last (selected) line in with black font on red background.

What I got: As you can see the font is now white and the background is partly red/blue.

Interestingly in the first two lines the setter was defining the foreground colors as expected. Even If I deactivate the first two setters for the foreground, the selected text in the row still stays white.

Why is this the case? How to define the desired coloring for the whole selection?


Solution

  • You should add handling of IsSelected for DataGridCell as well (in the same way with DataGridRow)

    <DataGrid.CellStyle>
        <Style TargetType="DataGridCell">
             <Style.Triggers>
                 <Trigger Property="IsSelected" Value="True">
                     <Setter Property="Foreground"  Value="Black" />
                     <Setter Property="Background"  Value="Red" />
                 </Trigger>
              </Style.Triggers>
        </Style>
    </DataGrid.CellStyle>