title: How to color certain header columns and certain cells of a datagrid wpf
body: <p>I was trying a example to color certain datagrids cell depending on a condition but do not know how to manage color of certain cells with code</p>
  
  <blockquote>
    <p>In the following code, the third column will be red if date is even</p>
  </blockquote>
  
  <p>I am using</p>
  
  <pre><code>public partial class MainWindow : Window
  {
      public MainWindow()
      {
          InitializeComponent();
          LoadSummaryMesh();
      }
  
      private void LoadSummaryMesh()
      {
          DataTable dt = new DataTable();
          dt.Columns.Add("c1");
          dt.Columns.Add("c2");
          dt.Columns.Add("xxx", typeof(DateTime));
          dt.Rows.Add("aa", "" , DateTime.Now.AddDays(0));
          dt.Rows.Add("bb", "" ,DateTime.Now.AddDays(1));
          dt.Rows.Add("cc", "" , DateTime.Now.AddDays(2));
          this.dataGrid1.DataContext = dt;
      }
  }
  
  public class MyBkColorConverter : IValueConverter
  {
      #region IValueConverter Members
  
      public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
      {
          DataRowView drv = value as DataRowView;
          if (drv != null)
          {
              DateTime dt = DateTime.Parse(drv[2].ToString());
              if (dt.Day % 2 == 0) //If it's a even number day.
                  return Brushes.Red;
          }
          return Brushes.White;
      }
  
      public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
      {
          throw new NotImplementedException();
      }
      #endregion
  }
  </code></pre>
  
  <p>and xaml</p>
  
  <pre><code>&lt;Window x:Class="SummaryMesh.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:local="clr-namespace:SummaryMesh" 
          Title="MainWindow" Height="350" Width="525"&gt;
      &lt;Window.Resources&gt;
          &lt;local:MyBkColorConverter x:Key="bkColorCvrt"/&gt;
          &lt;Style x:Key="LeftAlignmentColumnHeaderStyle" TargetType="{x:Type DataGridColumnHeader}" BasedOn="{StaticResource WrappedColumnHeaderStyle}"&gt;
      &lt;Setter Property="HorizontalContentAlignment" Value="Left"/&gt;
  &lt;/Style&gt;
  
  &lt;Style x:Key="soDataGrid_ColumnHeaderRotateStyle" TargetType="DataGridColumnHeader" &gt;
              &lt;Setter Property="ContentTemplate" &gt;
                  &lt;Setter.Value&gt;
                      &lt;DataTemplate&gt;
                          &lt;TextBlock TextWrapping="Wrap" Text="{Binding}"
                             FontWeight="Bold" Width="90"
                             VerticalAlignment="Center" TextAlignment="Center"
                             HorizontalAlignment="Center"&gt;
                      &lt;TextBlock.LayoutTransform&gt;
                          &lt;RotateTransform Angle="270" /&gt;
                      &lt;/TextBlock.LayoutTransform&gt;
                          &lt;/TextBlock&gt;
                      &lt;/DataTemplate&gt;
                  &lt;/Setter.Value&gt;
              &lt;/Setter&gt;
              &lt;Setter Property="HorizontalContentAlignment" Value="Center" /&gt;
          &lt;/Style&gt;
      &lt;/Window.Resources&gt;
  
      &lt;Grid&gt;
  
  
  
          &lt;DataGrid ItemsSource="{Binding}"  
                    AutoGenerateColumns="False"
                    ColumnHeaderStyle ="{StaticResource soDataGrid_ColumnHeaderRotateStyle}"
                    Margin="22,12,18,19" Name="dataGrid1" 
                       xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit"&gt;
  
  
  
  
              &lt;DataGrid.Columns&gt;
                  &lt;DataGridTextColumn Binding="{Binding c1}" Width="80"  Header="c1" /&gt;
                  &lt;DataGridTextColumn Binding="{Binding c2}" Width="80" Header="c2"/&gt;
                  &lt;DataGridTextColumn Binding="{Binding xxx}" Width="80"  Header="xxx"&gt;
                      &lt;DataGridTextColumn.CellStyle&gt;
                          &lt;Style TargetType="{x:Type DataGridCell}"&gt;
                              &lt;Setter Property="Background"&gt;
                                  &lt;Setter.Value&gt;
                                      &lt;Binding Converter="{StaticResource bkColorCvrt}"/&gt;
                                  &lt;/Setter.Value&gt;
                              &lt;/Setter&gt;
                          &lt;/Style&gt;
                      &lt;/DataGridTextColumn.CellStyle&gt;
                  &lt;/DataGridTextColumn&gt;
              &lt;/DataGrid.Columns&gt;
          &lt;/DataGrid&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>To get:</p>
  
  <p><a href="https://i.stack.imgur.com/zgHi3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zgHi3.png" alt="enter image description here"></a></p>
  
  <p>Now the question is how can I color only the second column header blue
  and how to color [second column,second row] yellow (using the datatable I am using) ?</p>
  
  <p>something similar to
  <a href="https://i.stack.imgur.com/8kLwU.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8kLwU.png" alt="enter image description here"></a></p>