目录

wpf中DataGrid组件每一行的背景色动态变化

目录

wpf中DataGrid组件每一行的背景色动态变化

背景描述:存在多个轧辊,其中有的轧辊是成对的,成对的辊ROLL_NO这个变量的值相同,有的轧辊是单个暂时没有配对的。成对的辊北京颜色交替突出显示,单个辊不需要设置背景色。

https://i-blog.csdnimg.cn/direct/09d4d559f2f549208695fe0ad0691f8a.png

实现: 换辊的时候给成对的辊分配相同的ROLL_NO,且ROLL_NO是表中最大的ROLL_NO+1。

int maxRollNo = DBHandle.GetRollNo();

if ((OnlineRollData[0].ROLL_NO != 0 && OnlineRollData[3].ROLL_NO != 0 && OnlineRollData[0].ROLL_NO != OnlineRollData[3].ROLL_NO) ||

OnlineRollData[0].ROLL_NO == 0 || OnlineRollData[3].ROLL_NO == 0)

{

int no = ++maxRollNo;

DBHandle.RollPairing(no, OnlineRollData[0].ROLL_ID);

DBHandle.RollPairing(no, OnlineRollData[3].ROLL_ID);

}

if ((OnlineRollData[1].ROLL_NO != 0 && OnlineRollData[2].ROLL_NO != 0 && OnlineRollData[1].ROLL_NO != OnlineRollData[2].ROLL_NO) ||

OnlineRollData[1].ROLL_NO == 0 || OnlineRollData[2].ROLL_NO == 0)

{

int no = ++maxRollNo;

DBHandle.RollPairing(no, OnlineRollData[1].ROLL_ID);

DBHandle.RollPairing(no, OnlineRollData[2].ROLL_ID);

}

if ((OnlineRollData[4].ROLL_NO != 0 && OnlineRollData[5].ROLL_NO != 0 && OnlineRollData[4].ROLL_NO != OnlineRollData[5].ROLL_NO) ||

OnlineRollData[4].ROLL_NO == 0 || OnlineRollData[5].ROLL_NO == 0)

{

int no = ++maxRollNo;

DBHandle.RollPairing(no, OnlineRollData[4].ROLL_ID);

DBHandle.RollPairing(no, OnlineRollData[5].ROLL_ID);

}

public static int GetRollNo()

{

int rollNo = 0;

try

{

OracleDataReader readData;

string cmdstr = “SELECT MAX(ROLL_NO) from ROLLDATA”;

readData = Utilities.GetDataReader(cmdstr);

while (readData.Read())

{

if (readData.GetValue(0) != DBNull.Value && readData.GetValue(0) != “”)

rollNo = Convert.ToInt32(readData.GetValue(0));

}

readData.Close();

readData.Dispose();

}

catch (Exception ex)

{

MessageBox.Show(“GetRollNo error” + ex.Message);

LoggingDriver.info(“GetRollNo error is {0}”, ex.Message);

}

return rollNo;

}

每组辊获取相同的ROLL_NO后,给DataGrid设置行样式,其中背景色使用转换器

https://i-blog.csdnimg.cn/direct/690b8152b5534427a3566ddcc83566dc.png

using NPOI.Util;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using System.Windows.Media;

namespace SBCSML2Client.Command
{
    public class PairIdToBrushConverter : IValueConverter
    {
        private static readonly Dictionary<int, string> _cache = new Dictionary<int, string>();
        int lastId = -1;
        string lastColor = "";
        int count = 0;
        private static readonly string[] _palette =
        {
            "#FFFFF0",//淡黄色
            "#F5FFFA"//淡蓝色
    };

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {   
            if (value is int id)
            {   
                if (!_cache.TryGetValue(id, out var brush))
                {
                    if (id != 0)
                    {            
                        if (id != lastId)
                        {
                            count++;
                            brush = _palette[count % 2];
                            lastId = id;
                            lastColor = brush;
                        }
                        else
                        {
                            brush = lastColor;
                        }
                    }
                }
                return brush;
            }
            return Brushes.Transparent;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            => throw new NotImplementedException();
    }
}

页面中添加 转换器 的资源

https://i-blog.csdnimg.cn/direct/7277ea2a1afc4cea99d840bcfb194b8e.png

https://i-blog.csdnimg.cn/direct/b6164381a37f4e8ab0095730df396eb6.png