I have an accounting app that determines when a customer is on credit hold, at risk of going on credit hold, or their account is in good standing.
My goal is to display the customer's name in the xamgrid, with red for hold, yellow at risk and green for good standing.
The logic to make this determination is done server side. I would like to do code behind logic for the column to determine what color to render the name based on a datamember with the risk status for the customer.
Is it possible to create a custom rule that runs codebehind? The examples I have scene are about if the value of the current column is greater than X, then use some style. I need to make the "name" column color determination from another column.
Is it possible to create a rule set that is generic and based on the current row collection like e.cell.row, have the rule fire, but leave the logic open so I can factor in the rotation of the earth if I feel like it in make the determination of how to style the column.
Thanks,
You could use conditional formatting to do this.
http://help.infragistics.com/Doc/Silverlight/2010.3/CLR4.0/?page=xamGrid_Conditional_Formatting.html
You would probably want to create a custom rule to run on your name column but look to your data object (and the field you want to see) and style based on that.
Attached is a quick sample showing how it could work.
This is comparing the value of the column to a constant, this will not solve my problem. Thanks though.
I guess I will keep answering my own posts...
To create a custom validation rule, you need to override the DiscreetRuleBase as below:
In this example if the contract is expiring in the next month, the background of the cell is yellow, if expired the background is red.
public class ContractExpiresRule : DiscreetRuleBase
{
protected override IConditionalFormattingRuleProxy CreateProxy()
return new ContractExpiresProxy();
}
public class ContractExpiresProxy : DiscreetRuleBaseProxy
protected override Style EvaluateCondition(object sourceDataObject, object sourceDataValue)
if (sourceDataValue == null)
return null;
SLE_Reference.VendorContractAssociations vca = (SLE_Reference.VendorContractAssociations)sourceDataObject;
Style style = new Style(typeof (CellControl));
//if expiring in the next month, show as yellow
//if expired, show in red
if (vca.Expires>DateTime.Now.AddMonths(-1))
style.Setters.Add(new Setter(CellControl.BackgroundProperty, "Yellow"));
else if (vca.Expires > DateTime.Now)
style.Setters.Add(new Setter(CellControl.BackgroundProperty, "Red"));
else
style.Setters.Add(new Setter(CellControl.BackgroundProperty, "White"));
return style;
Since I am using my own style properly, which also gives the ability to have the same rule make any number of style choices, I avoid having to setup the <*.Resources> in my xaml and passing the style into the function, via the StyleToApply in the <...ConditionalFormatCollection> In fact, in this example passing the style would not have worked.
Dont forget to set AllowConditionalFormatting to true.
Enjoy,
~David
See one of my other posts on how to create a valueconverter.