title: Binding to WPF DataGrid cell value from within template
body: <p>I have a collection of custom data objects that I display in DataGrid. The columns are created dynamically on runtime. Some of the columns are TemplateColumns which display the value as a progressbar with respective text in the TextBlock; the brogressbar itself is defined from Style:</p>
  
  <pre class="lang-cs prettyprint-override"><code>private string CreateProgressBarColumnTemplate(string fieldName)
  {
      StringBuilder CellTemp = new StringBuilder();
      CellTemp.Append("&lt;DataTemplate ");
      CellTemp.Append("xmlns='http://schemas.microsoft.com/winfx/");
      CellTemp.Append("2006/xaml/presentation' ");
      CellTemp.Append("xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'&gt;");
      CellTemp.Append(String.Format("&lt;ProgressBar Margin=\"0,1,0,0\" MinWidth=\"100\" MaxWidth=\"Infinity\" MaxHeight=\"Infinity\" Width=\"Auto\" Height=\"Auto\" HorizontalAlignment=\"Stretch\"  VerticalAlignment=\"Stretch\" Value=\"{{Binding {0}}}\" Style=\"{{StaticResource ProgressBarStyle}}\"/&gt;", fieldName));
      CellTemp.Append("&lt;/DataTemplate&gt;");
      return CellTemp.ToString();
  }
  
  private DataGridTemplateColumn CreateProgressBarTemplateColumn(string fieldName, string columnHeader)
  {
      DataGridTemplateColumn column = new DataGridTemplateColumn();
      column.CanUserSort = true;
      column.CanUserResize = false;
      column.Header = columnHeader;
      column.CellTemplate = (DataTemplate)XamlReader.Parse(CreateProgressBarColumnTemplate(fieldName)); //display template
      return column;
  }
  </code></pre>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;Style x:Key="ProgressBarStyle" TargetType="ProgressBar"&gt;
      &lt;Setter Property="Template"&gt;
          &lt;Setter.Value&gt;
              &lt;ControlTemplate TargetType="ProgressBar"&gt;
                  &lt;Border BorderBrush="#BBC6C4" BorderThickness="1" CornerRadius="5" Padding="1"&gt;
                      &lt;Grid x:Name="PART_Track" &gt;
                          &lt;Rectangle x:Name="PART_Indicator" HorizontalAlignment="Left" RadiusX="5" RadiusY="5"&gt;
                              &lt;Rectangle.Fill&gt;
                                  &lt;LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"&gt;
                                      &lt;GradientStop Color="#FF1D5666" Offset="1"/&gt;
                                      &lt;GradientStop Color="#FF09B6FF"/&gt;
                                  &lt;/LinearGradientBrush&gt;
                              &lt;/Rectangle.Fill&gt;
                          &lt;/Rectangle&gt;
                          &lt;TextBlock FontFamily="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=FontFamily}" 
                                     FontSize="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=FontSize}" 
                                     FontWeight="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=FontWeight}" 
                                     TextAlignment="Center" 
                                     Background="Transparent" 
                                     DataContext="{TemplateBinding Value}" 
                                     Foreground="{Binding Converter={StaticResource ValueToColor}}" x:Name="ProgressText" 
                                     Margin="0,-3,0,0" 
                                     Text="{Binding Converter={StaticResource DoubleToPercentage}}"
                                     /&gt;
                      &lt;/Grid&gt;              
                  &lt;/Border&gt;       
              &lt;/ControlTemplate&gt;
          &lt;/Setter.Value&gt;
      &lt;/Setter&gt;
  &lt;/Style&gt;
  </code></pre>
  
  <p>As you can see, the TextBlock DataContext is set to Value property of progressbar and the TextBlock.Text is set from it.</p>
  
  <p>The problem is that Value property can not be higher that MaxValue (Value seems to be set to MaxValue if I try assigning greater value), and my TextBlock.Text will always show 100% even if the actual cell value is greater than this.</p>
  
  <p>The problem is, how do I bind TextBlock.Text property to actual cell value (the property on my custom data object, which is dynamic) and not to a progressbar? </p>