title: WPF DataGrid cell string format as a style or template
body: <p>I tried several ways to factor out the format of a cell in a WPF DataGrid (the one provided with .NET 4):</p>
  
  <ul>
  <li>a data converter,</li>
  <li>the "StringFormat" binding property within a style,</li>
  <li>the "StringFormat" binding property within a data template.</li>
  </ul>
  
  <p>I will describe my attempts as it might help someone else, and I hope someone can give me advice to improve on these solutions. Note that I am fairly new to WPF...</p>
  
  <p>The expected behaviour is that the cell string is formatted to something specific like "1,234,567" for display, but it should be formatted as "1234567" (its default formatting) when editing the cell. When I tried to use a data converter, I did not find a way to use the default formatting when editing, so I focused my energy on styles and templates.</p>
  
  <p>With a style, the definition of the DataGridTextColumn goes like this:</p>
  
  <pre><code>&lt;DataGridTextColumn Header="Price (Style)" SortMemberPath="BILL_PRICE"&gt;
      &lt;DataGridTextColumn.ElementStyle&gt;
          &lt;Style TargetType="{x:Type TextBlock}"&gt;
              &lt;Setter Property="Text" Value="{Binding Path=BILL_PRICE, StringFormat={}{0:N0}}"/&gt;
          &lt;/Style&gt;
      &lt;/DataGridTextColumn.ElementStyle&gt;
      &lt;DataGridTextColumn.EditingElementStyle&gt;
          &lt;Style TargetType="{x:Type TextBox}"&gt;
              &lt;Setter Property="Text" Value="{Binding Path=BILL_PRICE}"/&gt;
              &lt;Setter Property="Padding" Value="0"/&gt;
              &lt;Setter Property="BorderThickness" Value="0"/&gt;
          &lt;/Style&gt;
      &lt;/DataGridTextColumn.EditingElementStyle&gt;
  &lt;/DataGridTextColumn&gt;
  </code></pre>
  
  <p>The behaviour is exactly what is expected. However, I cannot factor out this style and use it several times, because of the binding. To solve the factoring problem, I used a DataGridTemplateColumn and data templates. Here is my DataGridTemplateColumn definition:</p>
  
  <pre><code>&lt;DataGridTemplateColumn Header="Price (Template)" SortMemberPath="BILL_PRICE"&gt;
      &lt;DataGridTemplateColumn.CellTemplate&gt;
          &lt;DataTemplate&gt;
              &lt;ContentControl Content="{Binding BILL_PRICE}" Template="{StaticResource CurrencyCellControlTemplate}"/&gt;
          &lt;/DataTemplate&gt;
      &lt;/DataGridTemplateColumn.CellTemplate&gt;
      &lt;DataGridTemplateColumn.CellEditingTemplate&gt;
          &lt;DataTemplate&gt;
              &lt;ContentControl Content="{Binding BILL_PRICE, Mode=TwoWay}" Template="{StaticResource CurrencyCellEditingControlTemplate}"/&gt;
          &lt;/DataTemplate&gt;
      &lt;/DataGridTemplateColumn.CellEditingTemplate&gt;
  &lt;/DataGridTemplateColumn&gt;
  </code></pre>
  
  <p>And the ControlTemplate definition:</p>
  
  <pre><code>&lt;ControlTemplate x:Key="CurrencyCellControlTemplate" TargetType="ContentControl"&gt;
      &lt;TextBlock Margin="2,0,2,0" Padding="0" TextAlignment="Right"&gt;
          &lt;TextBlock.Text&gt;
              &lt;Binding RelativeSource="{RelativeSource TemplatedParent}" Path="Content" StringFormat="{}{0:N0}"/&gt;
          &lt;/TextBlock.Text&gt;
      &lt;/TextBlock&gt;
  &lt;/ControlTemplate&gt;
  
  &lt;ControlTemplate x:Key="CurrencyCellEditingControlTemplate" TargetType="ContentControl"&gt;
      &lt;TextBox Padding="0" BorderThickness="0" TextAlignment="Right"&gt;
          &lt;TextBox.Text&gt;
              &lt;Binding RelativeSource="{RelativeSource TemplatedParent}" Path="Content"/&gt;
          &lt;/TextBox.Text&gt;
      &lt;/TextBox&gt;
  &lt;/ControlTemplate&gt;
  </code></pre>
  
  <p>Using data templates solves the initial problem of factoring out DataGrid cell formatting, but using a control template brings up ergonomic and visual problems. For instance, the double-tab navigation caused by the control template (discussed in many other places), and the look of the editing text box (which I try to fix with border thickness, padding and the other property settings.)</p>
  
  <p>Specific questions related to this issue are :</p>
  
  <ul>
  <li>Can a data converter be used to format the string for display and use the default formatting for edition?</li>
  <li>Is it possible to factor out the DataGridTextColumn style while still being able to specify the binding source?</li>
  <li>Is there a way to use a DataGridTemplateColumn but simply make it look and feel like a DataGridTextColumn?</li>
  </ul>