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

RadGridView - Custom Cell Style Selector

1 Answer 521 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Lindsay
Top achievements
Rank 1
Lindsay asked on 07 Sep 2011, 06:58 PM
I've looked at several examples (see links below), my code compiles and runs fine, except that my page loads initially with no data, then goes blank after a few seconds. What's wrong? I know it's to do with my Style Selector, but I can't find any problem even after debugging the SelectStyle method.

C# Code for the Custom Style Selector
using System.Windows;
using System.Windows.Media;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;
 
namespace AzureDashboard.Class
{
    public class PercentAttainmentStyleSelector : StyleSelector
    {
        public override Style SelectStyle(object item, DependencyObject container)
        {
            var style = new Style(typeof(GridViewRow));
 
            var cell = (GridViewCell)container;
            ChartDataContainer rowData = (ChartDataContainer)item;
 
            if (rowData.Decimal1 > (decimal)0.8)
            {
                style.Setters.Add(new Setter(GridViewCell.BackgroundProperty, new SolidColorBrush(Colors.Yellow)));
            }
            else
            {
                style.Setters.Add(new Setter(GridViewCell.BackgroundProperty, new SolidColorBrush(Colors.Red)));
            }
 
            return style;
        }
    }
}

Method 1
- using a Custom Style Selector
<telerik:GridViewDataColumn Width="100" DataMemberBinding="{Binding Decimal1}" DataFormatString="#0%" IsGroupable="False" IsFilterable="False">
    <telerik:GridViewDataColumn.Header>
        <Grid>
            <TextBlock Text="Cumulative YTD Attainment" TextWrapping="Wrap"/>
        </Grid>
    </telerik:GridViewDataColumn.Header>
    <telerik:GridViewDataColumn.CellStyleSelector>
        <local:PercentAttainmentStyleSelector></local:PercentAttainmentStyleSelector>
    </telerik:GridViewDataColumn.CellStyleSelector>

Method 2
- using a Custom Style Selector as a Static Resource
<Grid x:Name="LayoutRoot" HorizontalAlignment="Left">
    <Grid.Resources>
        <local:PercentAttainmentStyleSelector x:Key="MyPercentAttainmentStyleSelector" />
    </Grid.Resources>

<telerik:GridViewDataColumn Width="100" DataMemberBinding="{Binding Decimal1}" CellStyleSelector="{StaticResource MyPercentAttainmentStyleSelector}" DataFormatString="#0%" IsGroupable="False" IsFilterable="False">
    <telerik:GridViewDataColumn.Header>
        <Grid>
            <TextBlock Text="Cumulative YTD Attainment" TextWrapping="Wrap"/>
        </Grid>
    </telerik:GridViewDataColumn.Header>                           
</telerik:GridViewDataColumn>

Method 3
- using a data template and dynamically binding the background)
- this works, but there's ugly padding around the label
<telerik:GridViewDataColumn Width="100" DataFormatString="#0%" IsGroupable="False" IsFilterable="False">
    <telerik:GridViewDataColumn.Header>
        <Grid>
            <TextBlock Text="Cumulative YTD Attainment" TextWrapping="Wrap"/>
        </Grid>
    </telerik:GridViewDataColumn.Header>
    <telerik:GridViewDataColumn.CellTemplate>
        <DataTemplate>
            <sdk:Label Padding="0" Height="30" Width="105" Content="{Binding Decimal1}" Background="{Binding BackgroundBrush}" Margin="0" />
        </DataTemplate>
    </telerik:GridViewDataColumn.CellTemplate>

Any ideas why the style selector is causing the page to go blank? I've debugged and got no exceptions.

Reference links:
Method 1:  http://csharping.com/binding/binding-the-background-color-of-a-radgridview-cell/
Method 2: http://blogs.telerik.com/blogs/posts/10-04-01/conditional_styles_and_templates_with_radgridview_for_silverlight_and_wpf.aspx
Method 3: http://www.telerik.com/community/forums/wpf/gridview/binding-background-of-a-cell.aspx


Cheers,
Lindsay

1 Answer, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 13 Sep 2011, 09:48 AM
Hello Lindsay,

I am sending you a sample project illustrating a possible implementation of CellStyleSelector. May you take a look at it and let me know whether you can reproduce the behavior you experience on it ?
  

Greetings,
Maya
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
GridView
Asked by
Lindsay
Top achievements
Rank 1
Answers by
Maya
Telerik team
Share this question
or