I create class for style selector as follows:
public class NegativeNumberStyle : StyleSelector {
public override Style SelectStyle(object item, DependencyObject container) {
GridViewCell cell = container as GridViewCell;
var value = item.GetType().GetProperty(cell.Column.UniqueName).GetValue(item, null);
if (value.GetType() == typeof(int) && ((int)value) < 0) return NegativeStyle;
if (value.GetType() == typeof(decimal) && ((decimal)value) < 0) return NegativeStyle;
return null;
}
public Style NegativeStyle { get; set; }
}
Then I add the resources to the grid on my page as follows:
<Grid.Resources>
<my:NegativeNumberStyle x:Key="negativeNumberStyle">
<my:NegativeNumberStyle.NegativeStyle>
<Style TargetType="telerik:GridViewCell" BasedOn="StaticResource GridViewCellStyle}">
<Setter Property="Background" Value="Red"/>
</Style>
</my:NegativeNumberStyle.NegativeStyle>
</my:NegativeNumberStyle>
</Grid.Recources>
Since I load columns dynamically I set Columns CellStyle property in code behind
GridViewDataColumn gridColumn = new GridViewDataColumn();
gridColumn.CellStyleSelector = new NegativeNumberStyle();
But negative numbers aren't use the proper style (not showing in red color). I debug the code and noticed that NegativeProperty always returns null.
What am I doing wrong?
Thanks