title: Datagrid (WPF) Column styling programmatically (not xaml)
body: <p>I have looked on SO but haven't found an exact answer to what I am looking for. I have a DataGrid view bound to a data source. I want to style columns programmatically after the window with the datagrid is visible. I also want to change it from time to time based on some behavior.</p>
  
  <p>I tried to use the DataGridTemplateColumn but whenever it runs it deletes the data from those columns. Also I don't get the Cell Style when I try to get it from Resources (i.e. its always null)</p>
  
  <pre><code>        private void StyleColumns()
      {
          // Replace the DueDate column with a custom template column.
          for (int i = 0; i &lt; 7; i += 2)
          {
              // Create a new template column.
              DataGridTemplateColumn templateColumn = new DataGridTemplateColumn();
              templateColumn.Header = m_DataGrid.Columns[i].Header;
              Style style = new Style();
              templateColumn.CellStyle = (Style)Resources["ColumnGone"];
              // ...
              // Replace the auto-generated column with the templateColumn.
              m_DataGrid.Columns[i] = templateColumn;
          }
      }
  </code></pre>
  
  <p>XAML is like this</p>
  
  <pre><code>                        &lt;DataGrid AutoGenerateColumns="True" x:Name="m_grfFileDataGrid" ItemsSource="{Binding cb.GRF}"
                                RowHeight="20" ColumnWidth="*"
                                AlternatingRowBackground="Beige"
                                SelectionUnit="CellOrRowHeader"
                                FontFamily="Consolas"
                                FontSize="12"
                                CanUserReorderColumns="False"
                                CanUserSortColumns="False"
                                CanUserAddRows="False"
                                CanUserDeleteRows="False"&gt;
                          &lt;DataGrid.Resources&gt;
                              &lt;Style TargetType="DataGridCell" x:Key="ColumnGone"&gt;
                                  &lt;Setter Property="Background" Value="SeaGreen"/&gt;
                              &lt;/Style&gt;
                              &lt;Style x:Key="DisabledColumn"&gt;
                                  &lt;Setter Property="DataGridColumn.CanUserResize"
                                          Value="False" /&gt;
                                  &lt;Setter Property="DataGridColumn.CanUserSort"
                                          Value="False" /&gt;
                                  &lt;Setter Property="DataGridColumn.CanUserReorder"
                                          Value="False" /&gt;
                                  &lt;Setter Property="DataGridColumn.CellStyle"
                                          Value="{StaticResource ColumnGone}" /&gt;
                              &lt;/Style&gt;
                          &lt;/DataGrid.Resources&gt;
                      &lt;/DataGrid&gt;
  </code></pre>
  
  <p>Any help on this would be appreciated. thanks</p>