I needed a way to use Styles for a GridViewRow along with a RowStyleSelector and combine that with existing RadGridView styles within my XAML. My problem was the Silverlight did not like me declaring my RowStyle and RowStyleSelector attributes within the RadGridView.
The first part was fairly easy. I could define a base style for all my RadGridView's as a StaticResource which could be applied to all my RadGridView's using the Style attribute for the RadGridView. This included using the existing Telerik Office_Blue theme.
Next came the combination of Styles for the rows and the use of the RowStyleSelectors. Again, I defined a StaticResource for the base GridViewRow Style:
After that I created my RowStyleSelector as another StaticResource ensuring that it is BasedOn the existing BaseGridViewRowStyle:
For reference, my StyleSelector code is as follows:
To successfully combine Style, RowStyle and RowStyleSelector the declaration of my RadGridView then became:
This also gave me an unexpected bonus. My RadGridViews were housed in a ContentTemplate for the RadTabControl. Before I applied this solution there would be a small but noticeable delay (<1.5 secs.), when clicking between the tabs, before the grid was displayed. Having applied this solution, the grid displays instantly and the only delay is waiting for the machine to redraw the grid layout correctly.
The first part was fairly easy. I could define a base style for all my RadGridView's as a StaticResource which could be applied to all my RadGridView's using the Style attribute for the RadGridView. This included using the existing Telerik Office_Blue theme.
<!-- Base Style for the RadGridView. Should be assigned to ALL RadGridViews -->
<
Style
x:Key
=
"BaseGridViewStyle"
TargetType
=
"telerik:RadGridView"
>
<
Setter
Property
=
"RowHeight"
Value
=
"18"
/>
<
Setter
Property
=
"FontSize"
Value
=
"10"
/>
<
Setter
Property
=
"telerik:StyleManager.Theme"
Value
=
"Office_Blue"
/>
</
Style
>
Next came the combination of Styles for the rows and the use of the RowStyleSelectors. Again, I defined a StaticResource for the base GridViewRow Style:
<!-- Base Style for the GridViewRow. Should be applied to ALL GridViewRows -->
<
Style
x:Key
=
"BaseGridViewRowStyle"
TargetType
=
"telerik:GridViewRow"
>
<
Setter
Property
=
"FontSize"
Value
=
"10"
/>
</
Style
>
After that I created my RowStyleSelector as another StaticResource ensuring that it is BasedOn the existing BaseGridViewRowStyle:
<
styles:GridViewRowStyle
x:Key
=
"rowStyles"
>
<
styles:GridViewRowStyle.MarkedForDeletionStyle
>
<!-- Since we are setting the RowStyle for individual rows we base it on the BaseGridViewRowStyle -->
<
Style
TargetType
=
"telerik:GridViewRow"
BasedOn
=
"{StaticResource BaseGridViewRowStyle}"
>
<
Setter
Property
=
"IsEnabled"
Value
=
"False"
/>
</
Style
>
</
styles:GridViewRowStyle.MarkedForDeletionStyle
>
</
styles:GridViewRowStyle
>
For reference, my StyleSelector code is as follows:
/// <
summary
>
/// Uses the StyleSelector interface to define how a row appears to the user
/// </
summary
>
public class GridViewRowStyle : StyleSelector
{
public override Style SelectStyle(object item, DependencyObject container)
{
if (item is ObjectDetail)
{
ObjectDetail detail = item as ObjectDetail;
// If the record is marked for removal then we can disable the row
if (detail.RemoveObject)
{
return MarkedForDeletionStyle;
}
}
return base.SelectStyle(item, container);
//return null;
}
public Style MarkedForDeletionStyle { get; set; }
}
To successfully combine Style, RowStyle and RowStyleSelector the declaration of my RadGridView then became:
<
telerik:RadGridView
Style
=
"{StaticResource BaseGridViewStyle}"
RowStyleSelector
=
"{StaticResource rowStyles}"
... >
...
...
</
telerik:RadGridView
>
This also gave me an unexpected bonus. My RadGridViews were housed in a ContentTemplate for the RadTabControl. Before I applied this solution there would be a small but noticeable delay (<1.5 secs.), when clicking between the tabs, before the grid was displayed. Having applied this solution, the grid displays instantly and the only delay is waiting for the machine to redraw the grid layout correctly.