title: DataGrid CellStyle based on global CellStyle
body: <p>I've a DataGrid and I want to change the background colours of individual cells. This is reasonably simple to do after some searching with xaml such as</p>
  
  <pre><code>&lt;DataGridTextColumn.CellStyle&gt;
      &lt;Style&gt;
          &lt;Setter Property="Border.Background" Value="{Binding Converter={StaticResource ImportTableBackgroundColorConverter},ConverterParameter=GotName}" /&gt;
      &lt;/Style&gt;
  &lt;/DataGridTextColumn.CellStyle&gt;
  </code></pre>
  
  <p>However, in an app-wide ResourceDictionary I also have</p>
  
  <pre><code>&lt;Style TargetType="DataGrid" x:Key="GlobalCellStyle"&gt;
  
      &lt;!-- Cell style --&gt;
      &lt;Setter Property="CellStyle"&gt;
          &lt;Setter.Value&gt;
              &lt;Style TargetType="DataGridCell"&gt;
  
                  &lt;!-- Single Click Editing --&gt;
                  &lt;EventSetter Event="PreviewMouseLeftButtonDown"
                           Handler="DataGridCell_PreviewMouseLeftButtonDown" /&gt;
                  &lt;EventSetter Event="KeyDown" Handler="DataGridCell_KeyDown" /&gt;
                  &lt;EventSetter Event="GotFocus" Handler="DataGridCell_GotFocus"/&gt;
  
                  &lt;!--body content datagrid cell vertical centering--&gt;
                  &lt;Setter Property="Template"&gt;
                      &lt;Setter.Value&gt;
                          &lt;ControlTemplate TargetType="{x:Type DataGridCell}"&gt;
                              &lt;Grid Background="{TemplateBinding Background}"&gt;
                                  &lt;ContentPresenter VerticalAlignment="Center" /&gt;
                              &lt;/Grid&gt;
                          &lt;/ControlTemplate&gt;
                      &lt;/Setter.Value&gt;
                  &lt;/Setter&gt;
              &lt;/Style&gt;
          &lt;/Setter.Value&gt;
      &lt;/Setter&gt;
  &lt;/Style&gt;
  </code></pre>
  
  <p>This sets all DataGrid cells to centre their content and, using some codebehind that goes with the same file, makes the cells go into edit mode on a single click. Specifying a new style locally loses this. If I try and specify the new local style based on the global, I get the exception <code>Can only base on a Style with target type that is base type 'IFrameworkInputElement'</code>.</p>
  
  <p>I've tried bringing the global DataGridCell style itself outside the global DataGrid style and get the same error. This is despite <a href="https://msdn.microsoft.com/en-us/library/dn902156.aspx" rel="nofollow">DataGridCell appearing to implement IFrameworkInputElement.</a></p>
  
  <p>Because I'm passing a parameter to the ValueConverter to let it identify which field the cell is displaying, I can't move my background colour stuff to the global style- I'd have to have the whole row background changing colour together. And copying the global style to each column declaration in my table, as well as possibly having to copy the codebehind as well, seems quite horrendous both initially and to maintain later.</p>
  
  <p>Does anyone know how I can either get the style inheritance to work, or to know what column I'm in when the ValueConverter is called?</p>