title: WPF datagrid: converter and StringFormat
body: <p>I have a standard (WPF toolkit) data grid.  Some of the columns (which are explicitly defined) have to be shown as percentages.  Some columns have to be shown in red if the values are below 0.  (The two sets of columns are not the same).  I tried to implement these requirements using a <code>StringFormat</code> and <code>Style</code>, respectively.  My XAML:</p>
  
  <pre><code>&lt;Window xmlns:local="clr-namespace:myNamespace"
          xmlns:tk="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit"&gt;
      &lt;Window.Resources&gt;
          &lt;local:ValueConverter x:Key="valueToForeground" /&gt;
          &lt;Style TargetType="{x:Type tk:DataGridCell}"&gt;
              &lt;Setter Property="Foreground"
                      Value="{Binding RelativeSource={RelativeSource Self}, Path=Content.Text, Converter={StaticResource valueToForeground}}" /&gt;
          &lt;/Style&gt;
      &lt;/Window.Resources&gt;
      &lt;Grid&gt;
          &lt;tk:DataGrid AutoGenerateColumns="False"
                       ItemsSource="{Binding Path=myClass/myProperty}"&gt;
              &lt;tk:DataGrid.Columns&gt;
                  &lt;tk:DataGridTextColumn Header="A"
                                         Binding="{Binding colA}" /&gt;
                  &lt;tk:DataGridTextColumn Header="B"
                                         Binding="{Binding colB, StringFormat=\{0:P\}}" /&gt;
                  &lt;tk:DataGridTextColumn Header="C"
                                         Binding="{Binding colC, StringFormat=\{0:P\}}" /&gt;
                  &lt;tk:DataGridTextColumn Header="D"
                                         Binding="{Binding colD, StringFormat=\{0:P\}}" /&gt;
              &lt;/tk:DataGrid.Columns&gt;
          &lt;/tk:DataGrid&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>And the relevant converter:</p>
  
  <pre><code>namespace myNamespace
  {
      public class ValueConverter : IValueConverter
      {
          public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
          {
              SolidColorBrush brush = new SolidColorBrush(Colors.Black);
  
              Double doubleValue = 0.0;
              if (value != null)
              {
                  if (Double.TryParse(value.ToString(), out doubleValue))
                  {
                      if (doubleValue &lt; 0)
                          brush = new SolidColorBrush(Colors.Red);
                  }
              }
              return brush;
          }
  
          public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
          {
              throw new NotImplementedException();
          }
      }
  }
  </code></pre>
  
  <p>I think it's all pretty standard, but the problem is that the converter gets the <code>Text</code> value after it's gone through the <code>StringFormat</code>, and at that point it's difficult to parse it correctly (since in reality, not all columns have the same format).  If I take out the <code>StringFormats</code>, the converter works fine and the text shows up in red.  Am I missing something obvious?  Is there an easy way to work around this?  The only thing that I can think of right now is moving the formatting into a different converter, and I'm not convinced that would work.</p>