如何根据列值设置 WPF DataGrid 行的一部分的背景颜色?
假设我有一个包含 5 列的 DataGrid,从客户类集合加载(使用 AutoGenerateColumns=True)。根据第一、第三和第四列中的值,我需要使用以下选项之一设置行的背景:
col num: 1 2 3 4 5
Option1: gray gray gray gray gray
Option2: gray red red gray gray
Option3: gray gray gray red red
Option4: gray blue blue blue blue
这需要为每一行单独完成。
我见过一些为行背景设置转换器的示例,但是如何设置单个单元格背景?
Say I have a DataGrid with 5 columns, loaded (using AutoGenerateColumns=True) from a custome class collection. Based on the value in the first, third and fourth columns, I need to set the background of the row with one of the following options:
col num: 1 2 3 4 5
Option1: gray gray gray gray gray
Option2: gray red red gray gray
Option3: gray gray gray red red
Option4: gray blue blue blue blue
This needs to be done for each row separately.
I've seen some examples of setting a converter for the row background, but how can I set individual cell backgrounds?
您可能需要使用自定义列,然后您可以将整行传递到转换器中,同时使用
ConverterParameter
指定当前列,这样您就可以比较所有值:我不完全是那个表你的,但你可能可以自己弄清楚如何在转换器中返回正确的值。
转换器骨架可能是这样的:
You probably need to use custom columns, then you can pass the whole row into a converter while using the
ConverterParameter
to specify the current column, that way you can compare all the values:I do not quite that table of yours but you probably can figure out how to return the correct value in the converter on your own.
Converter skeleton could be something like this: