小编end*_*ric的帖子

WPF通过条件以编程方式更改行背景色

我正在尝试使用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"));
        }
    }
Run Code Online (Sandbox Code Playgroud)

我的代码运行后,它正在工作。但是,当我向下滚动鼠标以查看以下行时,它也正在工作。然后,当我向上滚动鼠标以查看第一行时,我的代码无法正常工作。您可以在下面看到打开时间和滚动图片之间的时差。

一阶好 为什么令人困惑

谢谢。

c# wpf row colors

2
推荐指数
1
解决办法
1874
查看次数

标签 统计

c# ×1

colors ×1

row ×1

wpf ×1