根据前一行中的值更改数据网格行颜色

时间:2013-02-22 20:51:04

标签: c# wpf datagrid colors

我试图根据前一行中的值更改行的背景颜色。 如果新行具有相同的sourceIP值,我希望它具有相同的颜色。 如果它有不同的值我希望它有不同的颜色....等等 我希望每组的颜色交替。 将项目添加到数据网格中,使得具有相同sourceIP的行一个接一个地添加。

这是我写的代码:

Brush previousBrush = Brushes.GreenYellow;
string colorToggle;
void SortedTicketsGrid_LoadingRow(object sender,   System.Windows.Controls.DataGridRowEventArgs e)
    {
        // Get the DataRow corresponding to the DataGridRow that is loading...
        ticket RowDataContext = e.Row.DataContext as ticket;

        if (RowDataContext != null)
        {


            if (RowDataContext.sourceIP != colorToggle)
            {

                if (e.Row.Background == Brushes.AliceBlue)
                {
                    previousBrush = Brushes.GreenYellow;

                    e.Row.Background = previousBrush as Brush;

                }
                else
                {


                    previousBrush = Brushes.AliceBlue;

                    e.Row.Background = previousBrush as Brush;

                }
            }
            else
            {
                e.Row.Background = previousBrush;
            }

        }
        colorToggle = RowDataContext.sourceIP;
    }

我知道这可能是一个逻辑错误......

0 个答案:

没有答案
相关问题