Search code examples
c#wpfdatagridstylesdatagridcell

How to change background of DataGridCell when IsEditing=True in WPF


Setting background works fine for DataGridCheckBoxColumn but not for DataGridTextColumn. I set it for cell in resources:

<Style TargetType="{x:Type DataGridCell}">
    <Style.Triggers>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Background" Value="#ffff00" />
        </Trigger>

        <Trigger Property="IsEditing" Value="True">
            <Setter Property="BorderThickness" Value="1" />
            <Setter Property="BorderBrush" Value="#00ff00" />
            <Setter Property="Background" Value="#00ff00" />
        </Trigger>
    </Style.Triggers>
</Style>

Is there any solution for this issue?


Solution

  • You should add magic string:

    <SolidColorBrush x:Key="{x:Static SystemColors.WindowBrushKey}" Color="Transparent" />
    

    in your resources, for example in <Window.Resources>.

    In this case, when IsEditing="True" color is assigned by default (White), which is taken from the SystemColors. But then you need to explicitly set the color for the main panel or for Window.

    Or set this string in <DataGrid.Resources> with Background="White":

    <DataGrid Background="White" ...>
        <DataGrid.Resources>
            <SolidColorBrush x:Key="{x:Static SystemColors.WindowBrushKey}" Color="Transparent" />
        </DataGrid.Resources>
            ... 
    </DataGrid>