This is a migrated thread and some comments may be shown as answers.

Styles, StyleSelectors and BasedOn - a working solution

1 Answer 275 Views
GridView
This is a migrated thread and some comments may be shown as answers.
IsolatedStorage
Top achievements
Rank 1
IsolatedStorage asked on 03 Mar 2011, 03:46 AM
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" />
</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.

1 Answer, 1 is accepted

Sort by
0
Richard Newson
Top achievements
Rank 2
answered on 28 Oct 2011, 12:10 AM
I sort of get what you are saying - can you attach a small set of files as an example please?
Tags
GridView
Asked by
IsolatedStorage
Top achievements
Rank 1
Answers by
Richard Newson
Top achievements
Rank 2
Share this question
or