如何在gridview中应用这样的颜色逻辑?

时间:2013-06-24 10:01:59

标签: gridview windows-8 windows-runtime microsoft-metro

我正在构建gridview,gridview项源包含索引值和其他数据。我知道我可以在IValueConverter帮助将背景与index属性绑定的情况下应用背景,但我需要逻辑。我想要如下所示的gridview。

enter image description here

2 个答案:

答案 0 :(得分:0)

int i=0;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (i == 0)
    {
        e.Row.Cells[0].BackColor = System.Drawing.Color.Purple;
        e.Row.Cells[1].BackColor = System.Drawing.Color.Green;
        e.Row.Cells[2].BackColor = System.Drawing.Color.Red;
        i++;
    }
    else if (i == 1)
    {
        e.Row.Cells[0].BackColor = System.Drawing.Color.Green;
        e.Row.Cells[1].BackColor = System.Drawing.Color.Red;
        e.Row.Cells[2].BackColor = System.Drawing.Color.Purple;
        i++;
    }
    else if (i == 2)
    {
        e.Row.Cells[0].BackColor = System.Drawing.Color.Red;
        e.Row.Cells[1].BackColor = System.Drawing.Color.Purple;
        e.Row.Cells[2].BackColor = System.Drawing.Color.Green;
        i = 0;
    }
}

答案 1 :(得分:0)

终于找到了解决方案,我自己:)。

public class IndexToColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        SolidColorBrush _color = null;
        int _val = (int)value % 9;
        switch (_val)
        {
            case 1:
            case 3:
            case 8:
                _color = App.Current.Resources["GreenBrush"] as SolidColorBrush;
                break;

            case 0:
            case 5:
            case 7:
                _color = App.Current.Resources["OrangeBrush"] as SolidColorBrush;
                break;

            case 2:
            case 4:
            case 6:
                _color = App.Current.Resources["PurpleBrush"] as SolidColorBrush;
                break;
        }

        return _color;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }
}