如何在编辑单元格时更改WPF DataGrid单元格背景?

4
以下是如何在选中单元格时设置背景的示例,但实际上当我点击单元格进行编辑时,颜色会变化。是否有一个触发属性可以在单元格正在被编辑时使用?我希望背景不要改变。
<DataGrid Name="DG1" ItemsSource="{Binding}" SelectionUnit="Cell" >
    <DataGrid.CellStyle>
        <Style TargetType="DataGridCell" >
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Background" Value="SeaGreen"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </DataGrid.CellStyle>
</DataGrid>

回答自己的问题,看起来单元格背景色基于 SystemColors.WindowBrushKey。 通过像这样覆盖该资源 <SolidColorBrush x:Key="{x:Static SystemColors.WindowBrushKey}" Color="Red" /> 就可以解决这个问题。
2个回答

9

您可以在现有样式中为IsEditing状态添加另一个触发器。 然后,您可以在触发器内部设置DataGridCellControlTemplate

<Trigger Property="IsEditing" Value="True">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="DataGridCell">
                <TextBox Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content.Text, Mode=TwoWay, UpdateSourceTrigger=Default}"
                         HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Padding="0" BorderThickness="0" Background="SeaGreen"/>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Trigger>

2
不错的解决方案,但有一个问题:文本框没有自动聚焦。为了让文本框自动聚焦,需要在文本框的声明中添加以下属性:FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Self}}"。 - Mark
1
工作正常,但仅当所有列的类型为DataGridTextColumn时。如果有其他类型的列,例如带有CheckBox的DataGrigTemplateColumn,则在单击单元格后,它会变成TextBox而不是CheckBox。有没有建议如何在触发器中区分列类型? - dedpichto

0

这适用于DataGridTemplateColumn以及其他类型,如DataGridTextColumn、DataGridCheckboxColumn等:

<Style TargetType="{x:Type DataGridCell}">
    <Setter Property="Padding" Value="0" />
    <Setter Property="Margin" Value="0" />
    <Setter Property="BorderBrush" Value="Transparent" />
    <Setter Property="BorderThickness" Value="0" />
    <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGridCell}">
                <Border x:Name="CellBorder"
                        BorderBrush="{TemplateBinding BorderBrush}" 
                        BorderThickness="{TemplateBinding BorderThickness}" 
                        Background="{TemplateBinding Background}"
                        SnapsToDevicePixels="True">
                            <ContentPresenter HorizontalAlignment="Stretch" VerticalAlignment="Center" />
                </Border>
                <ControlTemplate.Triggers>                                            
                    <Trigger Property="IsEditing" Value="True">
                        <Setter TargetName="CellBorder" Property="BorderBrush" Value="Green" />
                        <Setter TargetName="CellBorder" Property="Background" Value="yellow" />
                        <Setter TargetName="CellBorder" Property="BorderThickness" Value="1" />
                        <Setter Property="Foreground" Value="Black" />
                    </Trigger>
                </ControlTemplate.Triggers>
             </ControlTemplate>
         </Setter.Value>
     </Setter>
</Style>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接