Setting a DataGrid background color based on the previous row

Posted by Zipper on Stack Overflow See other posts from Stack Overflow or by Zipper
Published on 2012-04-02T17:02:09Z Indexed on 2012/04/02 17:29 UTC
Read the original article Hit count: 158

Filed under:

I'm trying to setup a grid where I do column sorting, but I wanted to do zebra striping, only rather than every other row or every x rows, I want it to be based on the value of cells. i.e. All cells that contain 0 have a blue background, the next value would have a white background, the next value would be blue, etc....

The problem I have is that I can't seem to find where to actually do the setting of the background colors. I'm using a custom sorter and I tried setting it in there after I re-order the list and set the data source, but it appears that when the data source is set, that the rows don't exist yet. I tried using the DataContextChanged, but that event doesn't seem to be firing.

Here is what I have now.

namespace Foo.Bar
{
  public partial class FooBar
  {
    List<Bla> ResultList { get; set; }
    SolidColorBrush stripeOneColor = new SolidColorBrush(Colors.Gold);
    SolidColorBrush stripeTwoColor = new SolidColorBrush(Colors.White);

    //*********************************************************************************************
    public Consistency()
    {
      InitializeComponent();
    }

    //*********************************************************************************************
    override protected void PopulateTabWithData()
    {
      ResultList = GetBlas();
      SortAndGroup("Source");
    }

    //*********************************************************************************************
    private void SortAndGroup(string colName)
    {
      IOrderedEnumerable <Bla> ordered = null;
      switch (colName)
      {
        case "Source":
        case "ID":
          ordered = ResultList.OrderBy(r => r.Source).ThenBy(r => r.ID);
          break;
        case "Name":
          ordered = ResultList.OrderBy(r => r.Source).ThenBy(r => r.Name);
          break;
        case "Message":
          ordered = ResultList.OrderBy(r => r.Message);
          break;
        default:
          throw new Exception(colName);
      }

      ResultList = ordered.ThenBy(r => r.Source).ThenBy(r => r.ID).ToList(); // tie-breakers
      consistencyDataGrid.ItemsSource = null;
      consistencyDataGrid.ItemsSource = ResultList;
      ColorRows();
    }

    //*********************************************************************************************
    private void consistencyDataGrid_Sorting(object sender, System.Windows.Controls.DataGridSortingEventArgs e)
    {
      SortAndGroup(e.Column.Header.ToString());
      e.Handled = true;
    }

    private void ColorRows()
{
  for (var i = 0; i < ResultList.Count; i++)
  {
    var currentItem = ResultList[i];
    var row = myDataGrid.ItemContainerGenerator.ContainerFromItem(currentItem) as DataGridRow;
    if (row == null)
    {
      continue;
    }
    if (i > 0)
    {
      var previousItem = ResultList[i - 1];
      var previousRow = myDataGrid.ItemContainerGenerator.ContainerFromItem(previousItem) as DataGridRow;
      if (currentItem.Source == previousItem.Source)
      {
        row.Background = previousRow.Background;
      }
      else
      {
        if (previousRow.Background == stripeOneColor)
        {
          row.Background = stripeTwoColor;
        }
        else
        {
          row.Background = stripeOneColor;
        }
      }
    }
    else
    {
      row.Background = stripeOneColor;
    }
  }
}
    }
  }
}

© Stack Overflow or respective owner

Related posts about wpf