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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14785038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 07:26:44  来源:igfitidea点击:

How do you change WPF DataGrid cell background while editing the cell?

wpfdatagrid

提问by Aaron D.

The following is an example of how to set the background when the cell is selected, but when I actually click inside the cell to edit it the color changes. Is there a trigger property for when a cell is being edited? I'd like the background not to change.

以下是如何在选择单元格时设置背景的示例,但是当我实际在单元格内部单击以对其进行编辑时,颜色会发生变化。编辑单元格时是否有触发属性?我希望背景不要改变。

<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>

Answering my own question, it looks like the Cell background color is based off of SystemColors.WindowBrushKey. Overriding that resource like such <SolidColorBrush x:Key="{x:Static SystemColors.WindowBrushKey}" Color="Red" />did the trick. `

回答我自己的问题,看起来单元格背景颜色基于SystemColors.WindowBrushKey. 像这样覆盖那个资源<SolidColorBrush x:Key="{x:Static SystemColors.WindowBrushKey}" Color="Red" />就成功了。`

回答by Mash

You can add another trigger into your existing style for the IsEditingstate. Then you can set the ControlTemplatefor the DataGridCellinside of the trigger.

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

<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>

回答by Parisa

This works with DataGridTemplateColumn as well as other types like DataGridTextColumn, DataGridCheckboxColumn and etc.:

这适用于 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>