title: Conditional Formatting in WPF DataGridCell
body: <p>I am trying to apply some conditional formatting on a WPF DataGrid.The requirement is as follows. For any cell on the grid, if the content is an integer, the display format should be zero decimal places. If the contents have decimal places, then display them to the default value defined in the stringformat.</p>
  
  <p>Does anybody have any idea as to how I could achieve this kind of conditional formatting? I have written a converter which can check for the existence of decimals but I have been unable to work out how I would be able to apply this in my XAML to either my cell style or my text column.</p>
  
  <p>Cell Style - This is the style that I would like to define</p>
  
  <pre><code>&lt;Style x:Key="MyCellStyle" TargetType="DataGridCell"&gt;
      &lt;Setter Property="HorizontalContentAlignment" Value="Stretch"/&gt;
      &lt;Setter Property="VerticalContentAlignment" Value="Center"/&gt;
  &lt;/Style&gt;
  </code></pre>
  
  <p>Grid Column - I would to apply the style applied to this column</p>
  
  <pre><code>&lt;DataGridTextColumn x:Key="ColumnName"
                      Header="ColumnName"
                      SortMemberPath="MyColumnSort"
                      Binding="{Binding myColBinding, StringFormat={}{0:N2}}"
                      IsReadOnly="True"
                      Width="40" /&gt;
  </code></pre>
  
  <p>Converter - I would use this converter to determine whether this style gets applied.</p>
  
  <pre><code>public class NoDecimalConverter : IValueConverter
      {
          public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
          {
              int converted;
              int.TryParse(value.ToString(), out converted);
              value.ToString();
              return int.TryParse(value.ToString(), out converted);
          }
  
          public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
          {
              throw new NotImplementedException();
          }
      }
  </code></pre>
  
  <p>I tried to do it by adding a datatrigger to my cell style as below but I got an error stating "Cannot find the Style Property 'StringFormat' on the type 'System.Windows.Controls.DataGridCell'."</p>
  
  <pre><code>    &lt;Style x:Key="MyCellStyle" TargetType="DataGridCell"&gt;
      &lt;Setter Property="HorizontalContentAlignment" Value="Stretch"/&gt;
      &lt;Setter Property="VerticalContentAlignment" Value="Center"/&gt;
          &lt;Style.Triggers&gt;             
              &lt;DataTrigger Binding="{Binding NoDecimalConvertor}" Value="False" &gt;                 
                  &lt;Setter Property="StringFormat" Value="true" /&gt;             
              &lt;/DataTrigger&gt;         
          &lt;/Style.Triggers&gt;     
  &lt;/Style&gt;
  </code></pre>
  
  <p>I have been banging my head against this for days so any guidance would be appreciated as to the best way to approach this.</p>