Styles, StyleSelectors and BasedOn - a working solution

2 posts, 0 answers
  1. IsolatedStorage
    IsolatedStorage avatar
    15 posts
    Member since:
    Sep 2009

    Posted 02 Mar 2011 Link to this post

    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.

    <!-- 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" />

    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" />

    After that I created my RowStyleSelector as another StaticResource ensuring that it is BasedOn the existing BaseGridViewRowStyle:

    <styles:GridViewRowStyle x:Key="rowStyles">
            <!-- 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" />

    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}" ... >

    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.
  2. Richard Newson
    Richard Newson avatar
    17 posts
    Member since:
    Aug 2009

    Posted 27 Oct 2011 Link to this post

    I sort of get what you are saying - can you attach a small set of files as an example please?
Back to Top