[英]WPF DataGrid. Change single cell background
I have Data Table with around 66 columns and 4000 rows 我有大约66列和4000行的数据表
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 我是WPF的新手,实际上我已经实现了一些基于条件着色datagridview中的行但WPF作为组件DATA GRID
How to achieve cell based coloring based on cell value ? 如何基于细胞值实现基于细胞的着色? in WPF 在WPF中
earlier i was doing some thing like this in Win forms 早些时候我在Win表单中做了这样的事情
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;
}
You could use a value converter that implements the IValueConverter interface. 您可以使用实现IValueConverter接口的值转换器 。 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. 例如,如果单元格值= Good,则返回绿色,如果单元格值= bad,则返回红色。 You then need to bind the cell style property (eg background) to your cell value and set the converter as a parameter for that binding. 然后,您需要将单元格样式属性(例如背景)绑定到单元格值,并将转换器设置为该绑定的参数。
background = "{Binding ElementName=txtValue, Path=Text, Converter={StaticResource CellValueToColorConverter}}"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.