0

我正在使用DataGrid并尝试更改每隔一行的默认背景颜色(奇数行具有黄色,而偶数行具有白色),因为颜色与突出显示颜色太相似。

这是我的代码:

MyDataGrid.LoadingRow += delegate(object sender, DataGridRowEventArgs e)
{
    var currentRowContext = e.Row.DataContext;    
    if (currentRowContext.GetType()
                         .GetProperty("OBJECTID")
                         .GetValue(currentRowContext, null)
                         .ToString()]) % 2 == 0)
        {
            e.Row.Background = new SolidColorBrush(Colors.White);
        }
        else
        {
            e.Row.Background = new SolidColorBrush(
                                      new Color() { 
                                                    R = 235, 
                                                    G = 235, 
                                                    B = 0, 
                                                    A = 60 
                                                   });
};
MyDataGrid.UnloadingRow += delegate(object sender, DataGridRowEventArgs e)
{
    e.Row.Background = null;
};

一开始它显示正确,但是在我单击任何标题对记录进行排序后,颜色就混乱了(即,并非所有其他行都具有相同的颜色)。我发现设置此DataGrid行颜色真的很棘手,不知道是否有人已经解决了这个问题。 排序记录时如何保持交替颜色?

4

1 回答 1

2

这是想要达到的目标吗?(意味着每第二行都有自己的颜色)。在大多数相关的 wpf 控件中都有一个已实现的替代代码。

于 2012-08-16T16:35:54.367 回答