RowVirtualization cause incorrect background color for rows

吃可爱长大的小学妹 提交于 2019-11-29 12:24:51

This may be a bit too late for you since you apparently found a working solution for you, but hopefully it can help others. I had a similar problem just now and after some googling found out that the reason for this behaviour is that the default value for VirtualizationMode is Recycling. This means that item containers are reused and thus the coloring of the background will stick even if the item that occupies the container after scrolling says it should not be colored.

To solve this simply set the VirtualizationMode to Standard, like so:

<DataGrid VirtualizingStackPanel.VirtualizationMode="Standard" />

The containers will then be recreated and destroyed when you scroll, applying any changes you do in the LoadingRow event.

JD-V

Well as Eirik said it might be too late. but i would be glad if it can help others. You can do this EnableRowVirtualization = "True" (so it won't affect your performance in case of large data). And just code this on dgOrders_LoadingRow event.

private void dgOrders_LoadingRow(object sender, DataGridRowEventArgs e)
{

        OrderDetail item = e.Row.Item as OrderDetail; //parse data into your bject type which is 
        if (item != null)
        {
             if (item.Note== "Loss") //your condition
             {
                 e.Row.Background = Brushes.Red;
             }
             else                    //otherwise default color
             {
                 e.Row.Background = Brushes.White;
             }
        }
    }

As RowVirtualization is not working well for row background coloring, I added a column and colored it instead of whole row.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!