【问题标题】:How to change data grid header style or element style foreground in WPF如何在 WPF 中更改数据网格标题样式或元素样式前景
【发布时间】:2022-01-16 19:22:54
【问题描述】:

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

     <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);
        }

【问题讨论】:

    标签: c# wpf datagrid


    【解决方案1】:

    我可以看到您描述的行为有几个因素会影响标题/元素的颜色。
    在某些元素中使用画笔会冻结画笔,例如在某处添加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;
        }   
    }
    

    【讨论】:

    • 谢谢亲爱的 Rekshino 先生,它现在可以工作了。再次感谢您的帮助
    • 亲爱的朋友,你能看看这个问题吗,在网上找到它的解决方案让我很累,但我什么也没找到。 stackoverflow.com/questions/69664081/…
    • 没有MCVE 很难说。
    • 请问,我可以用您的电子邮件给您发送一个小项目示例吗?
    • 不,这不可能。
    猜你喜欢
    • 2014-01-05
    • 2014-10-21
    • 1970-01-01
    • 2010-10-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多