<
telerik:RadGridView
Name
=
"rgridProgramSearchResults"
AutoGenerateColumns
=
"True"
AutoGeneratingColumn
=
"rgridProgramSearchResults_AutoGeneratingColumn"
CanUserFreezeColumns
=
"False"
IsReadOnly
=
"True"
Grid.Column
=
"1"
ItemsSource
=
"{Binding}"
RowIndicatorVisibility
=
"Collapsed"
ShowGroupPanel
=
"False"
Margin
=
"0,0,0,30"
SelectionMode
=
"Multiple"
>
<
telerik:RadGridView.Columns
>
<
telerik:GridViewSelectColumn
UniqueName
=
"Select"
/>
</
telerik:RadGridView.Columns
>
</
telerik:RadGridView
>
and the style
<
me:ImportStyle
x:Key
=
"stadiumCapacityStyle"
>
<
me:ImportStyle.BigStadiumStyle
>
<
Style
TargetType
=
"telerik:GridViewCell"
>
<
Setter
Property
=
"Background"
Value
=
"Red"
/>
</
Style
>
</
me:ImportStyle.BigStadiumStyle
>
<
me:ImportStyle.SmallStadiumStyle
>
<
Style
TargetType
=
"telerik:GridViewCell"
>
<
Setter
Property
=
"Background"
Value
=
"Yellow"
/>
</
Style
>
</
me:ImportStyle.SmallStadiumStyle
>
</
me:ImportStyle
>
The auto generate event
private
void
rgridProgramSearchResults_AutoGeneratingColumn(
object
sender, GridViewAutoGeneratingColumnEventArgs e)
{
e.Column.CellStyleSelector = Application.Current.Resources[
"stadiumCapacityStyle"
]
as
StyleSelector;
}
and the class
public
class
ImportStyle : StyleSelector
{
public
override
Style SelectStyle(
object
item, DependencyObject container)
{
return
BigStadiumStyle;
}
public
Style BigStadiumStyle {
get
;
set
; }
public
Style SmallStadiumStyle {
get
;
set
; }
}
6 Answers, 1 is accepted
I have tested how the CellStyleSelector is applied to the column inside the AutoGeneratingColumn event and it was applied fine. May you please confirm what is the version that you use? Is the behaviour the same with the latest version?
Kind regards,Didie
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
MainWindow.xaml
<
Window
xmlns:telerik
=
"http://schemas.telerik.com/2008/xaml/presentation"
x:Class
=
"columns.MainWindow"
xmlns:me
=
"clr-namespace:columns"
Title
=
"MainWindow"
Height
=
"350"
Width
=
"525"
>
<
Grid
>
<
Grid.Resources
>
<
me:ImportStyle
x:Key
=
"stadiumCapacityStyle"
>
<
me:ImportStyle.BigStadiumStyle
>
<
Style
TargetType
=
"telerik:GridViewCell"
>
<
Setter
Property
=
"Background"
Value
=
"Red"
/>
</
Style
>
</
me:ImportStyle.BigStadiumStyle
>
</
me:ImportStyle
>
</
Grid.Resources
>
<
Grid.ColumnDefinitions
>
<
ColumnDefinition
/>
<
ColumnDefinition
/>
</
Grid.ColumnDefinitions
>
<
telerik:RadGridView
Name
=
"rgridProgramSearchResults"
AutoGenerateColumns
=
"True"
CanUserFreezeColumns
=
"False"
IsReadOnly
=
"True"
Grid.Column
=
"1"
AutoGeneratingColumn
=
"rgridProgramSearchResults_AutoGeneratingColumn"
ItemsSource
=
"{Binding}"
RowIndicatorVisibility
=
"Collapsed"
ShowGroupPanel
=
"False"
>
</
telerik:RadGridView
>
</
Grid
>
</
Window
>
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Windows;
using
System.Windows.Controls;
using
System.Windows.Data;
using
System.Windows.Documents;
using
System.Windows.Input;
using
System.Windows.Media;
using
System.Windows.Media.Imaging;
using
System.Windows.Navigation;
using
System.Windows.Shapes;
using
System.Data;
namespace
columns
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public
partial
class
MainWindow : Window
{
public
MainWindow()
{
InitializeComponent();
DataTable dt =
new
DataTable();
dt.Columns.Add(
"Col1"
);
dt.Columns.Add(
"Col2"
);
dt.Columns.Add(
"Col3"
);
DataRow dr = dt.NewRow();
dr[
"Col1"
] =
"row1Col1"
;
dr[
"Col2"
] =
"row1Col2"
;
dr[
"Col3"
] =
"row1Col3"
;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[
"Col1"
] =
"row2Col1"
;
dr[
"Col2"
] =
"row2Col2"
;
dr[
"Col3"
] =
"row2Col3"
;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[
"Col1"
] =
"row3Col1"
;
dr[
"Col2"
] =
"row3Col2"
;
dr[
"Col3"
] =
"row3Col3"
;
dt.Rows.Add(dr);
rgridProgramSearchResults.ItemsSource = dt;
}
private
void
rgridProgramSearchResults_AutoGeneratingColumn(
object
sender, Telerik.Windows.Controls.GridViewAutoGeneratingColumnEventArgs e)
{
e.Column.CellStyleSelector = Application.Current.Resources[
"stadiumCapacityStyle"
]
as
StyleSelector;
}
}
public
class
ImportStyle : StyleSelector
{
public
override
Style SelectStyle(
object
item, DependencyObject container)
{
return
BigStadiumStyle;
}
public
Style BigStadiumStyle {
get
;
set
; }
}
}
As I can see from your code snippet you have defined the "stadiumCapacityStyle" inside Grid.Resources, then in the code you try to access it from the Application resources, not from the current Grid's resources.
I have attached a sample project showing how the correct style is applied.
Didie
the Telerik team
This is most of what I was looking for.
It doesn't however show how you can return a style based on the column header.
I am trying to return a weekend and weekday style for the entire column.
The item in the handler is a row. It doesn't show the columns I am dynamically adding at runtime
foreach (DateTime day in EachDay(_startDate, (DateTime)_endDate))
{
var dateColumn = new GridViewExpressionColumn() { Header = day.ToString("dd MMM yyyy"), UniqueName = day.ToString("dd MMM yyyy"), DataFormatString = "{0:##,###}", IsReadOnly = true };
dateColumn.CellStyleSelector = Application.Current.Resources["FundingGridColumnStyle"] as StyleSelector;
columns.Add(dateColumn);
Expression<Func<FUNDING_TRADES, decimal?>> consideration = row => writeConsideration(row, day);
GridViewExpressionColumn considerationExpression = this.radGridView.Columns[day.ToString("dd MMM yyyy")] as GridViewExpressionColumn;
considerationExpression.Expression = consideration;
}
You can easily return a cell style based on the value of its column header. All you need to do is use the container parameter of the SelectStyle() method like so:
public
override
Style SelectStyle(
object
item, DependencyObject container)
{
if
(container
is
GridViewCell)
{
var cell = container
as
GridViewCell;
var columnHeader = cell.Column.Header;
}
return
bigStadium;
}
I hope this helps.
Regards,
Boris
Telerik
Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.