title: WPF DataGrid Cells Being Randomly Highlighted When Passed Through A Converter
body: <p>I am attempting to highlight all the cells that start with a given value in a text box that the user enters. I have looked at several examples, generated the following code, that works, sort of, till you start scrolling vertically.</p>
  
  <p>First we have the XAML, which utilizes a multi-binding:</p>
  
  <pre><code>&lt;DataGrid.CellStyle&gt;
      &lt;Style TargetType="{x:Type DataGridCell}"&gt;
          &lt;Setter Property="Background"&gt;
              &lt;Setter.Value&gt;
                  &lt;MultiBinding Converter="{StaticResource CellColor}"&gt;
                      &lt;Binding RelativeSource="{RelativeSource Mode=Self}"/&gt;
                      &lt;Binding Mode="OneWay" ElementName="contactFilterTextBox" Path="Text"/&gt;
                  &lt;/MultiBinding&gt;
              &lt;/Setter.Value&gt;
          &lt;/Setter&gt;
      &lt;/Style&gt;
  &lt;/DataGrid.CellStyle&gt;
  </code></pre>
  
  <p>Next the class that I created, which inherits from the IMultiValueConverter class:</p>
  
  <pre><code>public class ColorBasedOnFilterConverter : IMultiValueConverter
  {
      public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
      {
          System.Windows.Media.SolidColorBrush cellColor = Brushes.White;
          DataGridCell cellToAnalyze = (DataGridCell)values[0];
          TextBlock cellContents = (TextBlock)cellToAnalyze.Content;
          if (cellContents != null)
          {
              string cellValue = (string)cellContents.Text;
              string filterValue = values[1].ToString();
              if (!(String.IsNullOrEmpty(filterValue)))
              {
                  if (cellValue.ToUpper().StartsWith(filterValue.ToUpper()))
                  {
                      cellColor = Brushes.LightSalmon;
                  }
              }
          }
          return cellColor;
      }
      public object[] ConvertBack(
          object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
      {
          throw new NotSupportedException();
      }
  }
  </code></pre>
  
  <p>Initially, it works as I would expect as shown below:<img src="https://i.stack.imgur.com/quP2H.png" alt="Appropriate color classification by filter."></p>
  
  <p>The question then is when I scroll, values that are obviously are selected as shown in the image below?<img src="https://i.stack.imgur.com/GVe8u.png" alt="Inappropriate color classification by filter."></p>
  
  <p>I am very new to WPF, and surely I am doing something wrong, but I am baffled as to what it is. Any help would be most appreciated.</p>