WPF DataGrid. Change single cell background

北慕城南 提交于 2020-01-03 03:10:10

问题


I have Data Table with around 66 columns and 4000 rows

each Row comes to some category of some conditionally based coloring

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

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

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;
        }

回答1:


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}}"


来源:https://stackoverflow.com/questions/36545435/wpf-datagrid-change-single-cell-background

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