title: Conditional formatting of the font in a WPF Datagrid
body: <p>I have a WPF Datagrid populated using a Dataset. I am trying to change the font colour of the data in two columns. I have it working using </p>
  
  <pre><code>REMOVED OLD CODE
  </code></pre>
  
  <p>But it isn't holding the colours very well especially when there is scrolling on the grid. It is also slow.</p>
  
  <p>Is it possible to do this using an <code>IValueCoverter</code>  or is there some other more efficient way to achieve this?</p>
  
  <p><strong>EDIT</strong> </p>
  
  <p>I have tried to take a new approach to this problem. I have created a class to return a bool and then use that bool to determine whether the font will be green or red.</p>
  
  <p>CLASS</p>
  
  <pre><code>class EqualValuesColourConverter
  {
      public static void ChangeColours(int pQty, int pPri, int pTot, int gQty, int gPri, int gTot)
      {
          int iqty = pQty;
          int gqty = gQty;
          int iprice = pPri;
          int gprice = gPri;
          int itotal = pTot;
          int gtotal = gTot;
  
          bool fontColor = true;
  
          if ((iqty == gqty) &amp;&amp; (iprice == gprice) &amp;&amp; (itotal == gtotal)) fontColor = true;
          else fontColor = false;
  
      }
   }
  </code></pre>
  
  <p>CALL TO CLASS</p>
  
  <pre><code> string iqty = ((DataRowView)DgInvoiceLines.SelectedItem)["Inv_Quantity"].ToString();
              string gqty = ((DataRowView)DgInvoiceLines.SelectedItem)["Grn_Quantity"].ToString();
              string iprice = ((DataRowView)DgInvoiceLines.SelectedItem)["Inv_Price"].ToString();
              string gprice = ((DataRowView)DgInvoiceLines.SelectedItem)["Grn_Price"].ToString();
              string itotal = ((DataRowView)DgInvoiceLines.SelectedItem)["Inv_Total"].ToString();
              string gtotal = ((DataRowView)DgInvoiceLines.SelectedItem)["Grn_Total"].ToString();
  
              int pQty = int.Parse(iqty);
              int pPri = int.Parse(iprice);
              int pTot = int.Parse(itotal);
              int gQty = int.Parse(gqty);
              int gPri = int.Parse(gprice);
              int gTot = int.Parse(gtotal);
  
              EqualValuesColourConverter.ChangeColours(pQty, pPri, pTot, gQty, gPri, gTot);
  </code></pre>
  
  <p>XAML </p>
  
  <pre><code>&lt;DataGridTextColumn Width="61" Header="Inv_Quantity" Binding="{Binding Inv_Quantity}"&gt;
  &lt;DataGridTextColumn.CellStyle&gt;
      &lt;Style TargetType="{x:Type DataGridCell}"&gt;
          &lt;Setter Property="Foreground" Value="Green"/&gt;
          &lt;Style.Triggers&gt;
              &lt;DataTrigger Binding="{Binding EqualValuesColourConverter}" Value="False" &gt;
                  &lt;Setter Property="Foreground" Value="Red" /&gt;
              &lt;/DataTrigger&gt;
          &lt;/Style.Triggers&gt;
      &lt;/Style&gt;
  &lt;/DataGridTextColumn.CellStyle&gt;
  </code></pre>
  
  <p><br>
  
      
          
              
              
                  
                      
                  
              
          
      
  </p>
  
  <p>I am trying to get it to work in two columns but it will only change the font colour in one.</p>
  
  <p>Can anyone help? </p>