WPF DataGrid。更改单细胞背景 [英] WPF DataGrid. Change single cell background

查看:110
本文介绍了WPF DataGrid。更改单细胞背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有数据表,大约有66列和4000行

I have Data Table with around 66 columns and 4000 rows

每行都有某些类型的有条件的着色

each Row comes to some category of some conditionally based coloring

我对WPF非常新鲜,实际上我已经实现了一些基于条件的颜色在datagridview中的行,而WPF作为组件DATA GRID

I am very new to WPF actually i have implemented some condition based coloring the rows in datagridview but WPF as component DATA GRID

如何实现基于单元格的着色基于细胞值?在WPF中

How to achieve cell based coloring based on cell value ? in WPF

之前我在Win表单中做了一些这样的事情

earlier i was doing some thing like this in Win forms

public DataGridView colorGridview(DataGridView G)
        {
            string[] cellsrequired = {"Colnames1",""colname2};

            foreach (DataGridViewRow item in G.Rows)
            {

                foreach (DataGridViewCell cell in item.Cells)
                {
                    if (cellsrequired.Contains(cell.OwningColumn.HeaderText))
                    {
                        string str = cell.FormattedValue.ToString().Trim();
                        //  n / a
                        if (str != "N/A")// Or your condition 
                        {

                            if (str == "SKIP")
                            {
                                cell.Style.BackColor = Color.Orange;
                            }

                            else if (str == "FAIL")
                            {
                                cell.Style.BackColor = Color.Red;
                            }


                            else if (str == "INC")
                            {
                                cell.Style.BackColor = Color.Yellow;
                            }


                            else
                            {
                                cell.Style.BackColor = Color.SpringGreen;
                            }


                        }




                    }
                }

            }


            foreach (DataGridViewRow item in G.Rows)
            {
                if (object.Equals(item.Cells[35].Value, "FAIL"))
                {
                    var myparts = item.Cells[0].Value.ToString();

                    String[] CondtionsonCA = Getcondtion(myparts);

                    foreach (DataGridViewCell cell2 in item.Cells)
                    {
                        if (CondtionsonCA.Contains(cell2.OwningColumn.HeaderText))
                        {

                            string str = cell2.FormattedValue.ToString().Trim();
                            cell2.Style.BackColor = checkForColour(str);

                        }
                    }
                }
            }

            return G;
        }


推荐答案

您可以使用值转换器。我链接的文章做的很好,描述的过程,但基本上它是一个小类,需要任何输入值,检查它,然后返回你想要的。例如,如果单元格值= Good,则返回颜色为绿色,如果单元格值= bad,则返回颜色为红色。然后,您需要将单元格样式属性(例如背景)绑定到单元格值,并将转换器设置为该绑定的参数。

You could use a value converter that implements the IValueConverter interface. The article I've linked to does a good job of describing the process but basically it's a small class that takes any input value, inspects it, and then returns what ever you want. For example, if the cell value = Good, return the color green, if the cell value = bad return the color red. You then need to bind the cell style property (e.g. background) to your cell value and set the converter as a parameter for that binding.

background = "{Binding ElementName=txtValue, Path=Text, Converter={StaticResource CellValueToColorConverter}}"

这篇关于WPF DataGrid。更改单细胞背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆