【发布时间】: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);
}
【问题讨论】: