title: Is it possible to parameterize the binding on a DataGrid CellTemplate in a resource?
body: <p>I'm putting together some generic CellTemplate styles for a WPF grid (WPFToolKit DataGrid), and I’m not sure of the syntax to use to make the binding generic. So for example, I have this template, which turns the value red if the value is negative: </p>
  
  <pre><code>&lt;DataTemplate x:Key="RedNegativeCellTemplate"&gt;
      &lt;TextBlock Name="QuantityTextBox" Text="{Binding Quantity, StringFormat='c', Mode=OneWay}"/&gt;
      &lt;DataTemplate.Triggers&gt;
          &lt;DataTrigger Binding="{Binding Quantity, Converter={StaticResource SignConverter}}" Value="-1"&gt;
              &lt;Setter TargetName="QuantityTextBox" Property="Foreground" Value="Red"/&gt;
          &lt;/DataTrigger&gt;
      &lt;/DataTemplate.Triggers&gt;
  &lt;/DataTemplate&gt;
  </code></pre>
  
  <p>You’ll notice that this has a binding on the Text box to the column value ‘<em>Quantity</em>’ - i.e., the field/column that the binding comes from is explicit. </p>
  
  <p>So I can use this in my WPF DataGrid as follows:</p>
  
  <pre><code>&lt;sdk:DataGrid ItemsSource="{Binding MyDataSource}" AutoGenerateColumns="False"&gt;
      &lt;sdk:DataGrid.Columns&gt;
  
          &lt;sdk:DataGridTemplateColumn Header="Net Quantity" CellTemplate="{StaticResource RedNegativeCellTemplate}"/&gt;
  
      &lt;/sdk:DataGrid.Columns&gt;
  &lt;/sdk:DataGrid&gt;
  </code></pre>
  
  <p>But… what I’d really like to do is to have the field binding generic for the template, so that I can re-use the template as follows:</p>
  
  <pre><code>&lt;sdk:DataGrid ItemsSource="{Binding OrdersQuery}"AutoGenerateColumns="False"&gt;
      &lt;sdk:DataGrid.Columns&gt;
          &lt;sdk:DataGridTemplateColumn Header="Quantity" CellTemplate="{StaticResource RedNegativeCellTemplate}"/&gt;
          &lt;sdk:DataGridTemplateColumn Header="Price" CellTemplate="{StaticResource RedNegativeCellTemplate}"/&gt;
          &lt;sdk:DataGridTemplateColumn Header="Total" CellTemplate="{StaticResource RedNegativeCellTemplate}"/&gt;
      &lt;/sdk:DataGrid.Columns&gt;
  &lt;/sdk:DataGrid&gt;
  </code></pre>
  
  <p>Now, the problem here is that there doesn't seem to be a way to parameterize the binding in the CellTemplate. But I'm loath to have to have 3+ lines of controltemplate for <strong>every</strong> column that uses this template, as it clutters the XAML and makes it far less readable (not to mention the fact that if I decide to change the cell template to put a border around the textbox, I'd have to modify it in multiple places.</p>
  
  <p>So I'm thinking that the binding within the CellTemplate should look something like this (note that we're using '.' for the binding path):</p>
  
  <pre><code>            Text="{Binding Path=., StringFormat='c', Mode=OneWay}"
  </code></pre>
  
  <p>And then somehow set the datacontext from the DataGridTemplateColumn declaration – but I can’t see how to do it. </p>
  
  <p>Any idea how to do this? </p>