WPF Foreground Color in a DataGrid, Change via Code (C#)?

Bernd Riemke 41 Reputation points
2020-06-28T15:13:24.553+00:00

Hi,
I need to Change a Foreground Color in a DataGrid Line, here ist the XAML Code from the Line in the DataGrid:

                   <DataGridTemplateColumn Header="Nr " Width="38" CanUserSort="True" x:Name="SP001" >
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Label x:Name="SP001F" Height="50" Content="{Binding GrLaufendePosition}" Foreground="Black" Background="White" HorizontalAlignment="Center" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" BorderThickness="0" VerticalAlignment="Center"  />
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>

SP001.Foreground = "Red"; is wrong :-(
SP001F.Foreground = "Red"; is wrong too :-(

Have anyone a idea?

Best Regards

Bernd

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,685 questions
0 comments No comments
{count} votes

Accepted answer
  1. gekka 6,846 Reputation points MVP
    2020-06-28T22:05:07.79+00:00

    There are many ways to do this, but the easier way is to refer with DynamicResource.

    <DataGrid x:Name="dataGrid1" >
        <DataGrid.Resources>
            <SolidColorBrush Color="Black" x:Key="columnForeground"/>
        </DataGrid.Resources>
    
        <DataGrid.Columns>
            <DataGridTemplateColumn Header="Nr " Width="38" CanUserSort="True" x:Name="SP001" >
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate >
                        <Label x:Name="SP001F" 
                        Height="50" 
                        Content="{Binding GrLaufendePosition}"
    
                        Foreground="{DynamicResource columnForeground}" 
    
                        Background="White" 
                        HorizontalAlignment="Center" 
                        HorizontalContentAlignment="Center" 
                        VerticalContentAlignment="Center" 
                        BorderThickness="0" 
                        VerticalAlignment="Center"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>
    
    
    this.dataGrid1.Resources["columnForeground"] = Brushes.Red;
    
    2 people found this answer helpful.
    0 comments No comments

5 additional answers

Sort by: Most helpful
  1. Bernd Riemke 41 Reputation points
    2020-06-29T14:19:45.807+00:00

    I Change my Code:

                    <DataGrid.Resources>
    
                        <SolidColorBrush Color="Black" x:Key="columnForeground"/>
                        <SolidColorBrush Color="Transparent" x:Key="columnBackground"/>
    
                    </DataGrid.Resources>
    
    
    ...
    
                        <DataGridTemplateColumn Header="Nr " Width="38" CanUserSort="True" x:Name="SP001">
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <Label x:Name="SP001F" 
                                           Height="50" 
                                           Content="{Binding GrLaufendePosition}" 
    
                                           Foreground="{DynamicResource columnForeground}"  
                                           Background="{DynamicResource columnBackground}"  
    
                                           HorizontalAlignment="Center" 
                                           HorizontalContentAlignment="Center" 
                                           VerticalContentAlignment="Center" 
                                           BorderThickness="0" 
                                           VerticalAlignment="Center" 
                                           Padding="0" RenderTransformOrigin="0,0" TextOptions.TextHintingMode="Fixed"  />
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>
    
    …
    
                this.dg.Resources["columnForeground"] = Brushes.HotPink;
                this.dg.Resources["columnBackground"] = Brushes.LightBlue;
    

    What is here wrong?

    0 comments No comments

  2. Ken Tucker 5,846 Reputation points
    2020-06-29T10:13:56.15+00:00

    In a bound column for a datagrid in wpf if you want to change the color of some of the rows use a IValueConverter. This interface allows you to bind to a property and in this example change the brush used for the forground.

       public class ColorConverter : IValueConverter
       {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                int id = 0;
    
                if (value != null && int.TryParse(value.ToString(), out id))
               {
                     if (id == 3)
                     {
                          return new SolidColorBrush(Colors.Red);
                     }
               }
    
               return new SolidColorBrush(Colors.Black);
            }
    
    
             public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
             {
                   throw new NotImplementedException();
             }
       }
    

    To use this in the Xaml. First register it as a resource

       <Window.Resources>
             <local:ColorConverter x:Key="ColorConverter" />
       </Window.Resources>
    

    Then in the binding you can use the converter

           <DataGridTemplateColumn Header="Name" Width="380" CanUserSort="True">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Label Height="50"
                               Content="{Binding Name}"
    
                               Foreground="{Binding id, Converter={StaticResource ColorConverter}}"
    
                               Background="White"
                               HorizontalAlignment="Center"
                               HorizontalContentAlignment="Center"
                               VerticalContentAlignment="Center"
                               BorderThickness="0"
                               VerticalAlignment="Center" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
    
    0 comments No comments

  3. Bernd Riemke 41 Reputation points
    2020-06-29T10:14:22.113+00:00

    SUPER!
    That is what i Need :-)
    And the Background is then "columnBackground" … !?

    0 comments No comments

  4. Bernd Riemke 41 Reputation points
    2020-06-29T13:52:46.273+00:00

    But if i change the Colors then i become this (the Colors of the fields are now ok...)

    10897-xxx.png

    The Background is White!?
    This is NOT the db.Background Color...
    It must be anythink like ROW or Column Background?

    0 comments No comments