如何在 WPF 中更改数据网格 header 样式或元素样式前景

How to change data grid header style or element style foreground in WPF

我的 WPF 项目中有一个数据网格,这是我的 Xaml 和 c# 代码,为什么我不能更改其前景的列 header 样式和元素样式?有人知道我的代码哪里有问题吗?我的项目中有几个数据网格,我对所有这些数据网格都使用相同的代码,没有任何更改,但在某些数据网格中,它适用于列 header 样式和元素样式,或者仅适用于列 header 样式仅列元素样式,其余元素不适用于其中任何一个。

     <Window.Resources>
        <SolidColorBrush x:Key="DgColumnHeaderForeground" Color="Black"/>
        <SolidColorBrush x:Key="DgColumnElementForeground" Color="Black"/>
    </Window.Resources>
    <Grid>
        <DataGrid x:Name="DgSuggestion" AutoGenerateColumns="False" FlowDirection="RightToLeft" HorizontalAlignment="Left"
                  Height="326" Margin="10,158,0,0" VerticalAlignment="Top" Width="662" IsReadOnly="True">
            <DataGrid.Resources>
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#7FEEE30E" />
            </DataGrid.Resources>

            <DataGrid.VerticalGridLinesBrush>
                <SolidColorBrush Color="#FFBDB0B0" Opacity="0.7" />
            </DataGrid.VerticalGridLinesBrush>
            <DataGrid.HorizontalGridLinesBrush>
                <SolidColorBrush Color="#FFBDB0B0" Opacity="0.7" />
            </DataGrid.HorizontalGridLinesBrush>
            <DataGrid.Columns>
                <DataGridTextColumn Header="ID" Binding="{Binding SuggestionID}" FontSize="14" FontFamily="Calibri" MaxWidth="0" />
               <DataGridTextColumn Header="Date" Binding="{Binding SuggestionRequestDate, StringFormat=\{0:dd.MM.yyyy\}}" Width="Auto">
                    <DataGridTextColumn.HeaderStyle>
                        <Style TargetType="{x:Type DataGridColumnHeader}">
                            <Setter Property="FontFamily" Value="Calibri" />
                            <Setter Property="FontSize" Value="16" />
                            <Setter Property="FontWeight" Value="Bold" />
                            <Setter Property="HorizontalContentAlignment" Value="Center" />
                            <Setter Property="Foreground" Value="{DynamicResource DgColumnHeaderForeground}" />
                        </Style>
                    </DataGridTextColumn.HeaderStyle>

                    <DataGridTextColumn.ElementStyle>
                        <Style TargetType="{x:Type TextBlock}">
                            <Setter Property="FontFamily" Value="Calibri" />
                            <Setter Property="FontSize" Value="14" />
                            <Setter Property="FontWeight" Value="Bold" />
                            <Setter Property="HorizontalAlignment" Value="Center" />
                            <Setter Property="Foreground" Value="{DynamicResource DgColumnElementForeground}" />
                        </Style>
                    </DataGridTextColumn.ElementStyle>
                </DataGridTextColumn>
                
                <DataGridTextColumn Header="Dept" Binding="{Binding SuggestionDept}" Width="Auto">
                    <DataGridTextColumn.HeaderStyle>
                        <Style TargetType="{x:Type DataGridColumnHeader}">
                            <Setter Property="FontFamily" Value="Calibri" />
                            <Setter Property="FontSize" Value="16" />
                            <Setter Property="FontWeight" Value="Bold" />
                            <Setter Property="HorizontalContentAlignment" Value="Center" />
                            <Setter Property="Foreground" Value="{DynamicResource DgColumnHeaderForeground}" />
                        </Style>
                    </DataGridTextColumn.HeaderStyle>

                    <DataGridTextColumn.ElementStyle>
                        <Style TargetType="{x:Type TextBlock}">
                            <Setter Property="FontFamily" Value="Calibri" />
                            <Setter Property="FontSize" Value="14" />
                            <Setter Property="FontWeight" Value="Bold" />
                            <Setter Property="HorizontalAlignment" Value="Center" />
                            <Setter Property="Foreground" Value="{DynamicResource DgColumnElementForeground}" />
                        </Style>
                    </DataGridTextColumn.ElementStyle>
                </DataGridTextColumn>
                </DataGrid.Columns>
        </DataGrid>
    </Grid>

这是我背后的代码

 private void WinAppearanceMethod()
        {

            var brush5 = FindResource("DgColumnElementForeground") as SolidColorBrush;
            if (brush5.IsFrozen) brush5 = brush5.Clone();
            if (!string.IsNullOrEmpty(Default.dgContentFontColor)) brush5.Color = (Color)ColorConverter.ConvertFromString(Default.dgContentFontColor);

            var brush6 = FindResource("DgColumnHeaderForeground") as SolidColorBrush;
            if (brush6.IsFrozen) brush6 = brush6.Clone();
            if (!string.IsNullOrEmpty(Default.dgHeaderFontColor)) brush6.Color = (Color)ColorConverter.ConvertFromString(Default.dgHeaderFontColor);
        }

据我所知,您描述的行为有几个因素会影响 header/element 的颜色。
在某些元素中使用画笔会冻结画笔,例如在某处添加 TextBlock 并将 Foreground 设置为 "{DynamicResource DgColumnElementForeground}""{DynamicResource DgColumnHeaderForeground}" 将使笔刷冻结。同样使用DataGrid.
的元素刷 如果您在填充网格之前修改 DgColumnElementForeground,您将看到更改。
您的代码中有错误,您创建了一个冻结的对象,对其进行了修改,但没有将其设置回资源字典。

private void WinAppearanceMethod()
{

    var brush5 = FindResource("DgColumnElementForeground") as SolidColorBrush;
    if(brush5.IsFrozen)
        brush5 = brush5.Clone();
    if(!string.IsNullOrEmpty(Default.dgContentFontColor))
    {
        brush5.Color = (Color)ColorConverter.ConvertFromString(Default.dgContentFontColor);
        Resources["DgColumnElementForeground"] = brush5;
    }   

    var brush6 = FindResource("DgColumnHeaderForeground") as SolidColorBrush;
    if(brush6.IsFrozen)
        brush6 = brush6.Clone();
    if(!string.IsNullOrEmpty(Default.dgHeaderFontColor))
    {
        brush6.Color = (Color)ColorConverter.ConvertFromString(Default.dgHeaderFontColor);
        Resources["DgColumnHeaderForeground"] = brush6;
    }   
}