根据前一行设置DataGrid背景颜色

时间:2012-04-02 17:02:09

标签: c# wpf

我正在尝试设置一个我进行列排序的网格,但我想做斑马条纹,而不是每隔一行或每行x,我希望它基于前一行的值。即包含0的所有行都有蓝色背景,下一个值将为白色背景,下一个值为蓝色等等....

我遇到的问题是我似乎无法找到实际设置背景颜色的位置。我正在使用自定义排序器,我在重新排序列表并设置数据源之后尝试在其中设置它,但看起来在设置数据源时,行还不存在。我尝试使用DataContextChanged,但该事件似乎没有被触发。

这就是我现在所拥有的。

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;
    }
  }
}
    }
  }
}

1 个答案:

答案 0 :(得分:1)

为LoadingRow添加处理程序并将颜色逻辑放在那里:

bool isColorOne = false;
var previousValue = null;
private consistencyDataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
{
    // Check current value against previous value
    if (previousValue == e...)
    {
        previousValue = e...;
        isColorOne = !isColorOne;
    }
    if (isColorOne)
    {
        row.Background = stripeOneColor;
    }
    else
    {
        row.Background = stripeTwoColor;
    }
}

然后,您可以根据需要在排序时重置isColorOnepreviousValue的值。