title: How to change WPF DataGrid cell widget background color according to cell background color?
body: <h2>The background</h2>
  <p>I use VS2010, DataGrid (the one delivered with WPF) and I manually create rows and columns. I set various colors for rows, depending of their state (but for simplicity let's say it was yellow). It worked because datagrid used labels for displaying text, and when I set background for row, it is reflected in label widget as well.</p>
  <p>However, I couldn't ctrl+c (copy) content of the cell, so now I create custom template for columns and I use textbox to show the data.</p>
  <h2>The problem</h2>
  <p>Texbox blocks background of the cells, so in effect I get (for example) datagrid with white cells (textboxes) with yellow borders.</p>
  <h2>The question</h2>
  <p>How to make textboxes (this is my case) be aware of the background color of the cells? I tried to use a trick and set transparent brush for all textboxes but I still get white background in cells (textboxes).</p>
  <p>Current code:</p>
  <pre><code>        grid.BeginInit();
          grid.Columns.Clear();
  
  
          int i = 0;
          
          var glass_brush = new SolidColorBrush(Color.FromArgb(255,0,0,0));
  
          foreach (var db_col in query.FieldNames)
          {
              var template = new DataTemplate();
              var elemFactory = new FrameworkElementFactory(typeof(TextBox));
              elemFactory.SetBinding(TextBox.TextProperty, new Binding(String.Format(&quot;Visual[{0}]&quot;, i)));
              // make the background transparent -- it does not work though
              elemFactory.SetValue(TextBlock.BackgroundProperty,glass_brush);
              template.VisualTree = elemFactory;
  
              var col = new DataGridTemplateColumn();
              col.CellTemplate = template;
              col.IsReadOnly = true;
              col.Header = db_col;
              grid.Columns.Add(col);
              ++i;
          }
  
          {
              grid.Items.Clear();
  
  
              foreach (var db_row in diffs)
              {
                  var row = new DataGridRow();
                  row.Item = db_row.Item1;
                  row.Background = colors[db_row.Item2];
                  grid.Items.Add(row);
              }
          }
          grid.IsReadOnly = true;
  
          grid.EndInit();
  </code></pre>