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

Time:12-15

I have a data grid in my WPF project and this my Xaml and c# codes, why can't I change its foreground for column header style and element style? does anybody know where is wrong with my code? I have several data grid in my project and I use the same codes for all of them without any change but in some data grids it works for both column header style and element style or just for column header style either column element style only and in the rest it doesn't work for any of them.

     <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>

And this is my code behind

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

CodePudding user response:

As I can see the behavior you described have several factors which can impact the color of the header/element.
Using the brush in some elements do freeze the brush, e.g. adding somewhere TextBlock with a Foreground set to "{DynamicResource DgColumnElementForeground}" or "{DynamicResource DgColumnHeaderForeground}" will make brush frozen. Also using the brush by the elements of DataGrid.
In case you modify DgColumnElementForeground before the grid is filled you will see the change.
Error in your code, that having a frozen object you make a clone, modify it, but doesn't set it back to the resource dictionary.

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;
    }   
}
  • Related