在WPF中IsEditing = True时如何更改DataGridCell的背景

在WPF中IsEditing = True时如何更改DataGridCell的背景

问题描述:

设置背景对于 DataGridCheckBoxColumn 来说很好,但是对于 DataGridTextColumn 来说却不能。我将其设置为资源中的单元格:

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?

您应该添加 magic 字符串:

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

在您的资源中,例如在< Window.Resources>

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

在这种情况下,当默认分配 IsEditing = True 颜色时( White ),摘自 SystemColors 。但是随后您需要显式设置主面板或 Window 的颜色。

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.

或在< DataGrid.Resources> Background = White

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