I was sort of able to do what you suggested by adapting the CellStyleSelector solution that Maya demonstrated
, but it appears to have the same problem. Below is my code, modified to use CellStyleSelector. Note that I edited the ConditionalStyleSelector class to pass the GridViewCell object to the converter function, which may be the source of the problem. It appeared that I had to do it this way because the item object sent to the CellStyleSelector is the entire row, not the cell being tested, so I had no idea which column to check vs. the exclamation point.
When I tell it to test a particular cell in the item, that is, pass the entire row item and tell it to check a particular one of them, it appears to work correctly, but this is impractical because there will be an arbitrary number of columns in the report and I can't provide a separate CellStyleSelector function for each of them.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Telerik.Windows.Controls.GridView;
using Telerik.Data;
using Telerik.Windows;
using Telerik.Windows.Data;
using Telerik.Windows.Controls;
using System.Windows.Data;
namespace GrayGrid
{
public partial class MainPage : UserControl
{
public MainPage()
{
Loaded += new RoutedEventHandler(MainPage_Loaded);
InitializeComponent();
}
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn() { ColumnName = "C0", DataType = typeof(string) });
dt.Columns.Add(new DataColumn() { ColumnName = "C1", DataType = typeof(string) });
dt.Columns.Add(new DataColumn() { ColumnName = "C2", DataType = typeof(string) });
DataRow row = null;
row = dt.NewRow(); row["C0"] = "xx"; row["C1"] = "xx"; row["C2"] = "xx"; dt.Rows.Add(row);
row = dt.NewRow(); row["C0"] = "!xx"; row["C1"] = "xx"; row["C2"] = "xx"; dt.Rows.Add(row);
row = dt.NewRow(); row["C0"] = "xx"; row["C1"] = "xx"; row["C2"] = "xx"; dt.Rows.Add(row);
row = dt.NewRow(); row["C0"] = "xx"; row["C1"] = "xx"; row["C2"] = "!xx"; dt.Rows.Add(row);
row = dt.NewRow(); row["C0"] = "!xx"; row["C1"] = "xx"; row["C2"] = "xx"; dt.Rows.Add(row);
row = dt.NewRow(); row["C0"] = "xx"; row["C1"] = "xx"; row["C2"] = "xx"; dt.Rows.Add(row);
row = dt.NewRow(); row["C0"] = "!xx"; row["C1"] = "xx"; row["C2"] = "xx"; dt.Rows.Add(row);
row = dt.NewRow(); row["C0"] = "xx"; row["C1"] = "xx"; row["C2"] = "xx"; dt.Rows.Add(row);
row = dt.NewRow(); row["C0"] = "!xx"; row["C1"] = "xx"; row["C2"] = "xx"; dt.Rows.Add(row);
row = dt.NewRow(); row["C0"] = "xx"; row["C1"] = "xx"; row["C2"] = "xx"; dt.Rows.Add(row);
row = dt.NewRow(); row["C0"] = "xx"; row["C1"] = "xx"; row["C2"] = "!xx"; dt.Rows.Add(row);
row = dt.NewRow(); row["C0"] = "!xx"; row["C1"] = "xx"; row["C2"] = "xx"; dt.Rows.Add(row);
row = dt.NewRow(); row["C0"] = "xx"; row["C1"] = "xx"; row["C2"] = "xx"; dt.Rows.Add(row);
row = dt.NewRow(); row["C0"] = "!xx"; row["C1"] = "xx"; row["C2"] = "xx"; dt.Rows.Add(row); row = dt.NewRow(); row["C0"] = "xx"; row["C1"] = "xx"; row["C2"] = "xx"; dt.Rows.Add(row);
row = dt.NewRow(); row["C0"] = "!xx"; row["C1"] = "xx"; row["C2"] = "xx"; dt.Rows.Add(row);
row = dt.NewRow(); row["C0"] = "xx"; row["C1"] = "xx"; row["C2"] = "xx"; dt.Rows.Add(row);
row = dt.NewRow(); row["C0"] = "xx"; row["C1"] = "xx"; row["C2"] = "!xx"; dt.Rows.Add(row);
row = dt.NewRow(); row["C0"] = "!xx"; row["C1"] = "xx"; row["C2"] = "xx"; dt.Rows.Add(row);
row = dt.NewRow(); row["C0"] = "xx"; row["C1"] = "xx"; row["C2"] = "xx"; dt.Rows.Add(row);
row = dt.NewRow(); row["C0"] = "!xx"; row["C1"] = "xx"; row["C2"] = "xx"; dt.Rows.Add(row);
row = dt.NewRow(); row["C0"] = "xx"; row["C1"] = "xx"; row["C2"] = "xx"; dt.Rows.Add(row);
row = dt.NewRow(); row["C0"] = "!xx"; row["C1"] = "xx"; row["C2"] = "xx"; dt.Rows.Add(row);
row = dt.NewRow(); row["C0"] = "xx"; row["C1"] = "xx"; row["C2"] = "xx"; dt.Rows.Add(row);
row = dt.NewRow(); row["C0"] = "xx"; row["C1"] = "xx"; row["C2"] = "!xx"; dt.Rows.Add(row);
row = dt.NewRow(); row["C0"] = "!xx"; row["C1"] = "xx"; row["C2"] = "xx"; dt.Rows.Add(row);
row = dt.NewRow(); row["C0"] = "xx"; row["C1"] = "xx"; row["C2"] = "xx"; dt.Rows.Add(row);
row = dt.NewRow(); row["C0"] = "!xx"; row["C1"] = "xx"; row["C2"] = "xx"; dt.Rows.Add(row);
row = dt.NewRow(); row["C0"] = "xx"; row["C1"] = "xx"; row["C2"] = "xx"; dt.Rows.Add(row);
row = dt.NewRow(); row["C0"] = "!xx"; row["C1"] = "xx"; row["C2"] = "xx"; dt.Rows.Add(row);
row = dt.NewRow(); row["C0"] = "xx"; row["C1"] = "xx"; row["C2"] = "xx"; dt.Rows.Add(row);
row = dt.NewRow(); row["C0"] = "xx"; row["C1"] = "xx"; row["C2"] = "!xx"; dt.Rows.Add(row);
row = dt.NewRow(); row["C0"] = "!xx"; row["C1"] = "xx"; row["C2"] = "xx"; dt.Rows.Add(row);
row = dt.NewRow(); row["C0"] = "xx"; row["C1"] = "xx"; row["C2"] = "xx"; dt.Rows.Add(row);
row = dt.NewRow(); row["C0"] = "!xx"; row["C1"] = "xx"; row["C2"] = "xx"; dt.Rows.Add(row);
row = dt.NewRow(); row["C0"] = "xx"; row["C1"] = "xx"; row["C2"] = "xx"; dt.Rows.Add(row);
row = dt.NewRow(); row["C0"] = "!xx"; row["C1"] = "xx"; row["C2"] = "xx"; dt.Rows.Add(row);
row = dt.NewRow(); row["C0"] = "xx"; row["C1"] = "xx"; row["C2"] = "xx"; dt.Rows.Add(row);
row = dt.NewRow(); row["C0"] = "xx"; row["C1"] = "xx"; row["C2"] = "!xx"; dt.Rows.Add(row);
row = dt.NewRow(); row["C0"] = "!xx"; row["C1"] = "xx"; row["C2"] = "xx"; dt.Rows.Add(row);
row = dt.NewRow(); row["C0"] = "xx"; row["C1"] = "xx"; row["C2"] = "xx"; dt.Rows.Add(row);
row = dt.NewRow(); row["C0"] = "!xx"; row["C1"] = "xx"; row["C2"] = "xx"; dt.Rows.Add(row);
radGridView1.ItemsSource = dt;
radGridView1.Loaded += (s, f) =>
{
for (int col = 0; col <
radGridView1.Columns.Count
; col++)
{
Style
negativeStyle
=
new
Style();
negativeStyle.TargetType
=
typeof
(GridViewCell);
Setter
backGroundSetter
=
new
Setter();
backGroundSetter.Property
=
GridViewCell
.ForegroundProperty;
backGroundSetter.Value
=
new
SolidColorBrush(Colors.Gray);
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();
normalRule.Style
=
normalStyle
;
normalRule.Value
=
true
;
selector.Rules.Add(negativeRule);
selector.Rules.Add(normalRule);
ExclamationValueConverter
converter
=
new
ExclamationValueConverter();
selector.ConditionConverter
=
converter
;
radGridView1.Columns[col]
.CellStyleSelector
=
selector
;
}
};
}
}
public class ExclamationValueConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string converterValue = (string)((GridViewCell)value).Value;
if (converterValue != null)
{
return converterValue[0] == '!' ? false : true;
}
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
public class ConditionalStyleSelector : StyleSelector
{
public override System.Windows.Style SelectStyle(object item, System.Windows.DependencyObject container)
{
object
conditionValue
=
this
.ConditionConverter.Convert(container, 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;
}
}
}
}