我的 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);
}
uj5u.com熱心網友回復:
正如我所看到的,您描述的行為有幾個因素會影響標題/元素的顏色。
使用中的一些元素刷做凍結刷,如添加的某處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;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/381110.html
上一篇:有條件地顯示行詳細資訊
