Colouring cells in one grid column according to data value

Nov 27, 2008 at 1:42 AM

Hi,

I've been trying to work out the best method for applying a style to column, so that if the data is a positive value its foreground is green, or if negative is red. I  was looking at the loadingrowdetails event to apply the cellstyle to that column based on the contentvalue of the field. But i think columns[5].cellstyle, will apply it to the whole column, rather than the indiviual cell.

 

Any clues/tips, would be helpful.

 

Regards

 

Mark

Coordinator
Nov 27, 2008 at 3:43 AM
You should use DataGridColumn.CellStyle in which you should set the Background to binding with a converter to the data. Something like this...

<dg:DataGridTextColumn Binding="{Binding Path=ANumber}" >
    <dg:DataGridTextColumn.CellStyle>
        <Style TargetType={x:Type dg:DataGridCell}>
            <Setter Property="Background" Value="{Binding Path=ANumber, Converter=MyNumberToBrushConverter}" /> <!-- this converter should return red for negative else green-->
        </Style>
    </dg:DataGridTextColumn.CellStyle>
</dg:DataGridTextColumn>

Note that you should not set RowBackground or AlternatingRowBackground on DataGrid directly (locally) because they would have higher precedence and your cell style will never be picked up. If you want to set them you should create a style for DataGrid and set them in that style.
Nov 27, 2008 at 1:30 PM
Hi,

I assume I would have to write a custom class for the converter having looked at example codes for converters at:

1) http://www.codeplex.com/wpf/Thread/View.aspx?ThreadId=35309

2) http://blogs.msdn.com/vinsibal/archive/2008/09/16/wpf-datagrid-styling-rows-and-columns-based-on-header-conditions-and-other-properties.aspx

Regards

Mark
Nov 27, 2008 at 4:23 PM
Yep - a value converter is what you need. In the example given above:

<Setter Property="Background" Value="{Binding Path=ANumber, Converter=MyNumberToBrushConverter}" /> <!-- this converter should return red for negative else green-->

Your converter will have to take a value of whatever type 'ANumber' is, and output an object of the correct type for the Background property, which is of type Brush.

There is another alternative, add a DataTrigger to your style. These 'trigger' a collection of styles based on a property of your data. This allows you to change a combination of properties in one go. See the following article:

http://www.codeproject.com/KB/WPF/HiliteListViewItemsInWPF.aspx

Regards,
Colin E.
--
http://wpfadventures.wordpress.com/ - my WPF blog
http://www.codeproject.com/KB/WPF/WPFDataGridExamples.aspx - WPF DataGrid Practical Examples


Nov 27, 2008 at 9:08 PM
Hi Colin,

Thank you for you help and the example. That makes it clearer.

Would seem, that a data trigger referncing the underlying code would be simpler as only applying it to one column in the grid.

Regards

Mark
Nov 28, 2008 at 10:45 PM
Hi Colin,

Thank you for the tip and example, got it working with one column in the datagrid, and changed the setter to foreground, for the datagridcell.

Regards

Mark