We are trying to set the conditional style of a RadGridView cell programatically using the CellStyleSelector however, while no errors occurs the styling is not being reflected in the grid. Our columns in the RadGridView are populated by binding to an ObservableCollection and are not defined explicitly in XAML.
We're not sure what we are doing wrong and wanted to see if you could give us some pointers on what we're missing.
If the value in a cell is negative we would like the cell to be colored red and if the value is positive we'd like the cell to be the normal color.
//We are trying to set the style of a cell to red if it is negative and it is not working. Value converter is working fine though.
private
void
GridView_AutoGenerateColumn(
object
sender, Telerik.Windows.Controls.GridViewAutoGeneratingColumnEventArgs e)
{
if
(e.Column.UniqueName ==
"Something"
)
{
//Styles
// Negative Style
Style negativeStyle =
new
Style();
negativeStyle.TargetType =
typeof
(GridViewCell);
Setter backGroundSetter =
new
Setter();
backGroundSetter.Property = GridViewCell.BackgroundProperty;
backGroundSetter.Value = Brushes.Red;
negativeStyle.Setters.Add(backGroundSetter);
// Positive Style
Style normalStyle =
new
Style();
normalStyle.TargetType =
typeof
(GridViewCell);
//Selector and Rules
ConditionalStyleSelector selector =
new
ConditionalStyleSelector();
ConditionalStyleRule negativeRule =
new
ConditionalStyleRule();
negativeRule.Style = negativeStyle;
negativeRule.Value =
false
;
ConditionalStyleRule normalRule =
new
ConditionalStyleRule();
negativeRule.Style = normalStyle;
negativeRule.Value =
true
;
selector.Rules.Add(negativeRule);
selector.Rules.Add(normalRule);
NegativeValueConverter converter =
new
NegativeValueConverter();
selector.ConditionConverter = converter;
e.Column.CellStyleSelector = selector;
}
}
public
class
NegativeValueConverter : IValueConverter
{
#region IValueConverter Members
public
object
Convert(
object
value, Type targetType,
object
parameter, System.Globalization.CultureInfo culture)
{
SupplyAdjustmentModel converterValue = (SupplyAdjustmentModel)value;
if
(converterValue !=
null
)
{
return
converterValue.Value > 0 ?
true
:
false
;
}
return
null
;
}
public
object
ConvertBack(
object
value, Type targetType,
object
parameter, System.Globalization.CultureInfo culture)
{
throw
new
NotImplementedException();
}
#endregion
}
/// <summary>
/// Conditional style selector
/// Example Usage: To conditionally style the cell in a grid based on a set of rules (e.g. negative number = red cell)
/// </summary>
public
class
ConditionalStyleSelector : StyleSelector
{
public
override
System.Windows.Style SelectStyle(
object
item, System.Windows.DependencyObject container)
{
object
conditionValue =
this
.ConditionConverter.Convert(item,
null
,
null
,
null
);
foreach
(ConditionalStyleRule rule
in
this
.Rules)
{
if
(Equals(rule.Value, conditionValue))
{
return
rule.Style;
}
}
return
base
.SelectStyle(item, container);
}
List<ConditionalStyleRule> _Rules;
public
List<ConditionalStyleRule> Rules
{
get
{
if
(
this
._Rules ==
null
)
{
this
._Rules =
new
List<ConditionalStyleRule>();
}
return
this
._Rules;
}
}
IValueConverter _ConditionConverter;
public
IValueConverter ConditionConverter
{
get
{
return
this
._ConditionConverter;
}
set
{
this
._ConditionConverter = value;
}
}
}
public
class
ConditionalStyleRule
{
object
_Value;
public
object
Value
{
get
{
return
this
._Value;
}
set
{
this
._Value = value;
}
}
Style _Style;
public
Style Style
{
get
{
return
this
._Style;
}
set
{
this
._Style = value;
}
}
}