Wpf 基于全局CellStyle的DataGrid CellStyle

Wpf 基于全局CellStyle的DataGrid CellStyle,wpf,xaml,datagrid,Wpf,Xaml,Datagrid,我有一个数据网格,我想改变单个单元格的背景颜色。这在使用xaml进行一些搜索后相当简单,例如 <DataGridTextColumn.CellStyle> <Style> <Setter Property="Border.Background" Value="{Binding Converter={StaticResource ImportTableBackgroundColorConverter},ConverterParameter=Go

我有一个数据网格,我想改变单个单元格的背景颜色。这在使用xaml进行一些搜索后相当简单,例如

<DataGridTextColumn.CellStyle>
    <Style>
        <Setter Property="Border.Background" Value="{Binding Converter={StaticResource ImportTableBackgroundColorConverter},ConverterParameter=GotName}" />
    </Style>
</DataGridTextColumn.CellStyle>

然而,在应用程序范围的ResourceDictionary中,我也有

<Style TargetType="DataGrid" x:Key="GlobalCellStyle">

    <!-- Cell style -->
    <Setter Property="CellStyle">
        <Setter.Value>
            <Style TargetType="DataGridCell">

                <!-- Single Click Editing -->
                <EventSetter Event="PreviewMouseLeftButtonDown"
                         Handler="DataGridCell_PreviewMouseLeftButtonDown" />
                <EventSetter Event="KeyDown" Handler="DataGridCell_KeyDown" />
                <EventSetter Event="GotFocus" Handler="DataGridCell_GotFocus"/>

                <!--body content datagrid cell vertical centering-->
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type DataGridCell}">
                            <Grid Background="{TemplateBinding Background}">
                                <ContentPresenter VerticalAlignment="Center" />
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Setter.Value>
    </Setter>
</Style>

这会将所有DataGrid单元格设置为其内容的中心,并使用同一文件中的某些代码隐藏,使单元格在单击一次后进入编辑模式。在本地指定新样式将丢失此功能。如果尝试基于全局指定新的本地样式,则会出现异常
只能基于目标类型为基类型“IFrameworkInputElement”的样式

我尝试将全局DataGridCell样式本身置于全局DataGrid样式之外,但得到了相同的错误。尽管如此

因为我要向ValueConverter传递一个参数,让它识别单元格显示的字段,所以我不能将背景颜色移到全局样式-我必须让整行背景一起改变颜色。将全局样式复制到我的表中的每个列声明,以及可能还必须复制代码隐藏,无论是最初还是以后维护,都显得非常可怕


是否有人知道如何让样式继承工作,或者知道调用ValueConverter时我在哪一列?

您可能只需要使用BasedOn:

<Style BasedOn="{StaticResource GlobalCellStyle}">
    <Setter Property="Border.Background" Value="{Binding Converter={StaticResource ImportTableBackgroundColorConverter},ConverterParameter=GotName}" />
</Style>

非常奇怪,也就是说,我不明白为什么原始方法会失败(尽管DataGridCell显然实现了IFrameworkInputElement,因为我可以将前者转换为后者),但是如果继承的样式与它继承的样式在同一个ResourceDictionary中定义,它就会工作。i、 e

<Style TargetType="DataGridCell" x:Key="GlobalCellStyle">

    <!-- Your DataGrid Cell style definition goes here -->
    <!-- Single Click Editing -->
    <EventSetter Event="PreviewMouseLeftButtonDown"
                         Handler="DataGridCell_PreviewMouseLeftButtonDown" />
    <EventSetter Event="KeyDown" Handler="DataGridCell_KeyDown" />
    <EventSetter Event="GotFocus" Handler="DataGridCell_GotFocus"/>

    <!--body content datagrid cell vertical centering-->
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGridCell}">
                <Grid Background="{TemplateBinding Background}">
                    <ContentPresenter VerticalAlignment="Center" />
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<conv:ImportTableBackgroundColorConverter x:Key="ImportTableBackgroundColorConverter" />

<Style BasedOn="{StaticResource GlobalCellStyle}" TargetType="DataGridCell" x:Key="DOBCellStyle">
   <Setter Property="Border.Background" Value="{Binding Converter={StaticResource ImportTableBackgroundColorConverter},ConverterParameter=GotDOB}" />
</Style>


在某些情况下可能对其他人有用。

这就是导致异常的原因-这就是我试图将本地风格建立在全局基础上的方式。但是谢谢你的建议。设置单元格样式的TargetType?不,当我“尝试将全局DataGridCell样式本身置于全局DataGrid样式之外”时,我不得不这样做,它也做了同样的事情