我尝试使用LoadingRow来实现程序化地更改行颜色。它按照我的需要工作。但问题在于滚动条。当我在datagrid中使用滚动条时,我的公式会再次运行,导致我得到愚蠢的有序行颜色。
这是我的代码。我正在尝试根据第17列的值更改颜色。
private void dataGrid1_LoadingRow(object sender, DataGridRowEventArgs e)
{
DataGridRow row = e.Row;
DataRowView rView = row.Item as DataRowView;
if (rView != null && rView.Row.ItemArray[17].ToString().Contains("1"))
{
renk++;
}
if (renk % 2 == 0)
{
e.Row.Background = (SolidColorBrush)(new BrushConverter().ConvertFrom("#FF0000"));
}
else
{
e.Row.Background = (SolidColorBrush)(new BrushConverter().ConvertFrom("#ffffff"));
}
}
我的代码运行后正常。但是当我向下滚动鼠标查看以下行时,它也能正常工作。然后,当我向上滚动鼠标查看第一行时,代码就不能很好地工作了。您可以在下面的图片中看到打开时间和滚动后的不同之处。
谢谢。