0
我正在使用DataGrid
并尝试更改每隔一行的默认背景色(奇数行具有黄色,偶数行具有白色),因为颜色与高亮颜色太相似。如何将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
行的颜色真的是绝招,不知道是否有人已经解决了这个问题。 如何在排序记录时保持交替的颜色?
谢谢,但我不认为这就是我的意思。我想要的是奇数行总是具有相同的颜色(例如,白色),并且偶数行号具有相同的颜色(例如,黄色),没有任何字段按任何字段排序。顺便说一句,你是什么意思在WPF控制实施的代码? – lwconquer 2012-08-16 18:08:01
那么,这正是替代做的。 :) – 2012-08-16 18:26:04
妈的:)。这真是一个我不知道的很好的解决方案。这为我节省了很多时间。德米特里,非常感谢你的帮助。 – lwconquer 2012-08-16 18:58:42