title: How to set background of a datagrid cell during AutoGeneratingColumn event depending on its value?
body: <p>I'm still fighting with manipulation of cell backgrounds so I'm asking a new question.</p>
  <p>A user &quot;H.B.&quot; wrote that I can actually set the cell style during the <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.datagrid.autogeneratingcolumn.aspx" rel="noreferrer">AutoGeneratingColumn</a> event - <a href="https://stackoverflow.com/questions/5549617/change-datagrid-cell-colour-based-on-values/5551986#comment23145839_5551986">Change DataGrid cell colour based on values</a>. The problem is that I'm not sure how to do it.</p>
  <p><strong>What I want:</strong>
  Set different <strong>background colours</strong> for each cell depending on its value. If the value is <code>null</code> I also want it <strong>not</strong> to be <strong>clickable</strong> (focusable I guess).</p>
  <p><strong>What I have / I'm trying to do:</strong></p>
  <pre><code>private void mydatagrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
  {
      foreach (Cell cell in e.Column)
      {
          if (cell.Value &lt; 1)
          { 
              cell.Background = Color.Black; 
              cell.isFocusable = false; 
          } 
          else
          {
              cell.Background = Color.Pink;
          }
      }
  }
  </code></pre>
  <p>This is just the pseudocode. Is something like this is possible during column auto-generation and if so, how can I edit my code so it will be valid?</p>
  <p>I read about value convertors but I want to know if it's somehow possible programmatically, without writing XAML.</p>
  <p>Please understand that I'm still a beginner to C#/WPF/DataGrid.</p>
  <h2>Solution part1:</h2>
  <p>I used the answer I accepted. Just put it into</p>
  <pre><code>&lt;Window.Resources&gt; 
  &lt;local:ValueColorConverter x:Key=&quot;colorConverter&quot;/&gt;
          &lt;Style x:Key=&quot;DataGridCellStyle1&quot; TargetType=&quot;{x:Type DataGridCell}&quot;&gt; 
              &lt;Setter Property=&quot;Padding&quot; Value=&quot;5&quot;/&gt;
              &lt;Setter Property=&quot;Template&quot;&gt;
                  &lt;Setter.Value&gt;
                      &lt;ControlTemplate TargetType=&quot;{x:Type DataGridCell}&quot;&gt;
                          &lt;Border Padding=&quot;{TemplateBinding Padding}&quot; BorderBrush=&quot;{TemplateBinding BorderBrush}&quot; BorderThickness=&quot;{TemplateBinding BorderThickness}&quot; SnapsToDevicePixels=&quot;True&quot;&gt;
                              &lt;Border.Background&gt;
                                  &lt;MultiBinding Converter=&quot;{StaticResource colorConverter}&quot;&gt;
                                      &lt;Binding RelativeSource=&quot;{RelativeSource AncestorType=DataGridCell}&quot; Path=&quot;Content.Text&quot;/&gt;
                                      &lt;Binding RelativeSource=&quot;{RelativeSource AncestorType=DataGridCell}&quot; Path=&quot;IsSelected&quot;/&gt;
                                  &lt;/MultiBinding&gt;
                              &lt;/Border.Background&gt;
                              &lt;ContentPresenter SnapsToDevicePixels=&quot;{TemplateBinding SnapsToDevicePixels}&quot;/&gt;
                          &lt;/Border&gt;
                      &lt;/ControlTemplate&gt;
                  &lt;/Setter.Value&gt;
              &lt;/Setter&gt;
          &lt;/Style&gt;
  &lt;/Window.Resources&gt;
  </code></pre>
  <p>And made for it a <code>MultiBinding</code> convertor so I can also set the background color for selected cells.</p>
  <h3>Problem:</h3>
  <p>Now I only have to solve the problem of setting focus of empty cells. Any hints?</p>
  <pre><code>  &lt;Style.Triggers&gt;
          &lt;Trigger Property=&quot;HasContent&quot; Value=&quot;False&quot;&gt;
              &lt;Setter Property=&quot;Focusable&quot; Value=&quot;False&quot;/&gt;
          &lt;/Trigger&gt;
      &lt;/Style.Triggers&gt;
  </code></pre>
  <p>This doesn't work. I had empty strings in the empty cells, but they are filled with ´null´s so it should work, right? Or what am I doing wrong :| ?</p>
  <h2>Solution part 2:</h2>
  <p>So the code above won't work as long as the cell value is a ´TextBox´ so I decided to find another way to deal with it which can be found in my answer here: <a href="https://stackoverflow.com/a/16673602/2296407">https://stackoverflow.com/a/16673602/2296407</a></p>
  <p>Thanks for trying to help me :)</p>