Hello. This is yet another question about making the negative cells in my datagrid red. I found a couple of posts concerning negative values, but the solutions don't quite fit my requirements. I would like all of the cells in my grid which are negative to have a red foreground. I would like this to happen regardless of the names of the fields because the names and numbers of the fields may change at runtime. I would also like this to happen whether I set auto-generated-columns to true or false.
I found this post http://xceed.com/CS/forums/post/17155.aspx that provides a solution that colors cells the way I'd like (although it has no code in it). I would have to add fields to my datasource that look like IsFieldAPositive. This is not an ideal solution for a few reasons.
* First: This is a hack. If I add a numeric column at some point in the future, I will need to remember to add a IsFieldXPositive field as well. I will also need to edit my XAML code to make room for my new column whether or not my columns are autogenerated.
* Second: This is not reusable code. Every time I need to add a new grid in the future, I will need to add extra fields to its datasource's numeric fields and then edit the XAML code to use these new fields. Ideally, when I add a new grid to my project, I should simply have to add a line or two of code to the XAML file. One benefit of such a solution would be if I decide to change the color of the cells in the future, from Red to DarkRed, I would only need to edit just one line of code.
* Third: This code does not work if I won't know the column names until runtime. It also won't work if I don't know the number of columns that will be in my grid until runtime.
I also found this blog post http://xceed.com/CS/blogs/techside/archive/2011/07/06/datacell-styling-vs-cellcontenttemplate.aspx that does something similar to what I'd like. It colors the values in two columns based on their relative values. Higher values in a row are green and lower values in a row are red. This solution is a little bit closer to what I'd like, but it still has problems.
* Again I will run in to problems when I add numeric columns in the future. I like that with this solution, I don't need to add extra columns to my datasource though.
* Again the code is less than reusable.
* Again this code will not work if I don't know the names of my numeric columns nor how many numeric columns I will have:
I tried to tailor the second solution to my needs, but ran into an obstacle. My C# code is similar to the MultiValueColorConverter code in the blog post I linked. My XAML code looks like this:
<Style TargetType="{x:Type xcdg:DataCell}">
<Setter Property="Foreground">
<Setter.Value>
<MultiBinding Converter="{StaticResource negativeRedType}">
<Binding RelativeSource="{RelativeSource Self}" Path="ParentColumn.FieldName"/>
<Binding Path="." />
</MultiBinding>
</Setter.Value>
</Setter>
</Style>
The reason this code doesn't work is because that while it targets datacells, the binding path gives me an empty datacell.
Is there some generic code I can use that looks at all datacells regardless of their fieldnames, and then checks that the values can be converted to decimals, and then sets the foreground color based on the value?
P.S. The HTML controls in this "Write a New Post" page aren't working for me. I'm using Chrome.