C# 覆盖数据网格前景

C# 覆盖数据网格前景,c#,wpf,datagrid,C#,Wpf,Datagrid,我想覆盖WPF DataGrid中选定DataGrid行的前景色 对于每个列,在定义中设置前景 <DataGrid.Columns><DataGridTextColumn Foreground="Black"/> 我创建了一个新的DataGridCell样式。但这只适用于未设置前景色的列 <Style.Triggers> <Trigger Property="IsSelected" Value="True">

我想覆盖WPF DataGrid中选定DataGrid行的前景色

对于每个列,在定义中设置前景

<DataGrid.Columns><DataGridTextColumn Foreground="Black"/>

我创建了一个新的DataGridCell样式。但这只适用于未设置前景色的列

    <Style.Triggers>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Background" Value="BlueViolet" />
            <Setter Property="Foreground" Value="White" />
        </Trigger>
    </Style.Triggers>
</Style>


在WPF中,有一种称为。请按照链接了解完整的详细信息,但简而言之,
DependencyProperty
s可以从许多不同的来源进行更新:
动画、样式、代码隐藏等。有一个列表(在链接页面中)将所有这些来源从最重要到最不重要排序

更重要的源比不太重要的源具有优先级,并且可以更改不太重要的源设置的值

<DataGridTextColumn Foreground="Black" />

这个链接将来对我非常有用,谢谢。但我需要补充的是,为了工作,您需要obv。要将TargetType=“{x:Type DataGridCell}”BasedOn=“{StaticResource{x:Type DataGridCell}}”添加到标头。感谢更新,但我怀疑您是否也需要使用
BasedOn
属性,由于
TargetType
已经指向默认的
Style
。嗨@Sheridan,快速增强问题:为什么第一行有效,第二行无效?(创建完全在代码中):column.Foreground=brusks.Red;添加(新的Setter(Control.ForegroundProperty,brushs.Red));column.CellStyle=CellStyle;忘了它吧,我的错误是我设置了一个元素样式,在此之前覆盖了cellstyle。
<DataGridTextColumn ... >
    <DataGridTextColumn.CellStyle>
        <Style>
            <Setter Property="Foreground" Value="Black"/>
        </Style>
    </DataGridTextColumn.CellStyle>
</DataGridTextColumn>