wpf 如何在 XAML 中覆盖 Datagrid 单元格样式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13625125/
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 06:25:19  来源:igfitidea点击:

How to override Datagrid cell style in XAML?

wpf

提问by sony

I have a style defined in app.xaml as below:

我在 app.xaml 中定义了一个样式,如下所示:

<Style TargetType="DataGridCell">
        <Setter Property= "BorderThickness" Value="1"/>            
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="DataGridCell">
                            <Grid Height="18">
                                <Border BorderBrush="Black" BorderThickness=".2" >
                                    <Border x:Name="border" Background="#992288ff" BorderBrush="Black" BorderThickness=".2">
                                      ......
                      ......
                  ......
                                    </Border>
                                </Border>
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Trigger>
     </Style.Triggers>

In my form, I have a style defined for one cell. The code is below:

在我的表单中,我为一个单元格定义了一种样式。代码如下:

 <DataGridTextColumn Width="*" Header="Status" Binding="{Binding Status, Mode=TwoWay}" IsReadOnly="True" >
                    <DataGridTextColumn.CellStyle>
                        <Style TargetType="DataGridCell">
                            <Setter Property="Foreground">
                                <Setter.Value>
                                    <Binding Converter="{StaticResource FGColorKey}"/>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </DataGridTextColumn.CellStyle>
                </DataGridTextColumn>

Here I am defining only the foreground style only. But the style I defined in the app.xaml for the cell template is not applied for this cell. please see the image attached below:

这里我只定义了前景样式。但是我在 app.xaml 中为单元格模板定义的样式不适用于这个单元格。请看下面的图片:

enter image description here

在此处输入图片说明

回答by Peter Hansen

By setting the CellStyleproperty on the column, that style will be used for every cell in that column, and the system will no longer care about the style you have defined as a resource .

通过CellStyle在列上设置属性,该样式将用于该列中的每个单元格,系统将不再关心您定义为资源的样式。

To apply both styles, you have to base the one in the cellstyle on the one in your resources in app.xaml.

要应用这两种样式,您必须将 cellstyle 中的一种基于 app.xaml 中资源中的一种。

You do that by setting the BasedOnproperty on the style like this:

您可以通过BasedOn在样式上设置属性来做到这一点:

<DataGridTextColumn.CellStyle>
    <Style TargetType="DataGridCell" BasedOn="{StaticResource {x:Type DataGridCell}}">
        <Setter Property="Foreground">
            <Setter.Value>
                <Binding Converter="{StaticResource FGColorKey}"/>
            </Setter.Value>
        </Setter>
    </Style>
</DataGridTextColumn.CellStyle>