Hi,
I am using a RadGridView to bind a DataTable. In that table I have dynamically generated columns. That's why I chose to set AutoGenerateColumns=true.
I am now looking for a way to style the cells in depending on the properties of the object within the cell of the DataTable.
What I tried until now:
- I tried using a style selector as described here: https://www.telerik.com/forums/cell-style-based-on-cell-value-with-autogeneratecolumns-true. I set the style selector in OnAutoGeneratingColumn. Rest of the solution is as in the examples application. But unfortunatly the converter I wrote does not get an item of my type, but a type DataRowView.
- I tried this solution as well: https://www.telerik.com/forums/radgridview-celltemplate-on-dynamic-columns#3678858. More or less with the same result. I only have a DataRowView in the DataTemplate to bind.
The first method seem the most promissing to me, so here is some code for that:
<UserControl.Resources>
<Style x:Key="PilotStyle"
TargetType="telerik:GridViewCell">
<Setter Property="Background"
Value="Magenta" />
</Style>
<Style x:Key="ExpansionStyle"
TargetType="telerik:GridViewCell">
<Setter Property="Background"
Value="Aqua" />
</Style>
<valueConverters:RodTimeTypeConverter x:Key="converter" />
<local:RodTableCellStyleSelector x:Key="selector"
ConditionConverter="{StaticResource converter}">
<local:RodTableCellStyleSelector.Rules>
<local:ConditionalStyleRule Style="{StaticResource PilotStyle}">
<local:ConditionalStyleRule.Value>
<system:Boolean>True</system:Boolean>
</local:ConditionalStyleRule.Value>
</local:ConditionalStyleRule>
<local:ConditionalStyleRule Style="{StaticResource ExpansionStyle}">
<local:ConditionalStyleRule.Value>
<system:Boolean>False</system:Boolean>
</local:ConditionalStyleRule.Value>
</local:ConditionalStyleRule>
</local:RodTableCellStyleSelector.Rules>
</local:RodTableCellStyleSelector>
</UserControl.Resources>
<telerik:RadGridView Grid.Row="1"
CanUserSortColumns="False"
CanUserSelectColumns="False"
CanUserReorderColumns="False"
CanUserDeleteRows="False"
CanUserGroupColumns="False"
CanUserSelect="False"
IsFilteringAllowed="False"
ShowColumnSortIndexes="False"
ShowGroupPanel="False"
RowHeight="45"
AutoGeneratingColumn="GridViewDataControl_OnAutoGeneratingColumn"
ItemsSource="{Binding RodTimesTable.DefaultView}">
<telerik:RadGridView.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="FontSize"
Value="20" />
</Style>
</telerik:RadGridView.Resources>
</telerik:RadGridView>
public class RodTableCellStyleSelector : StyleSelector
{
public override Style SelectStyle(object item, DependencyObject container)
{
var conditionValue = this.ConditionConverter.Convert(item, null, null, null);
foreach (var rule in this.Rules.Where(rule => Equals(rule.Value, conditionValue)))
{
return rule.Style;
}
return base.SelectStyle(item, container);
}
List<ConditionalStyleRule> rules;
public List<ConditionalStyleRule> Rules
{
get
{
return this.rules ??= new List<ConditionalStyleRule>();
}
}
public IValueConverter ConditionConverter { get; set; }
}
public class ConditionalStyleRule
{
public object Value { get; set; }
public Style Style { get; set; }
}
private void GridViewDataControl_OnAutoGeneratingColumn(object sender, GridViewAutoGeneratingColumnEventArgs e)
{
var grid = (RadGridView)sender;
var dataGrid = (grid.ItemsSource as DataView)?.Table;
e.Column.Header = dataGrid?.Columns[e.Column.Header.ToString()].Caption;
if (e.Column.Header != null && (e.Column.Header.ToString().StartsWith("Pilot") || e.Column.Header.ToString().StartsWith("Expansion")) )
{
e.Column.CellStyleSelector = (StyleSelector)this.FindResource("selector");
}
}
I would really appreciate any help.
Thanks in advance.