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

Generic celll style selector

1 Answer 25 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Mark
Top achievements
Rank 1
Mark asked on 13 Jun 2012, 09:27 AM
Is it possible to create a generic cell style selector. Example if the content of the cell is negative (assume the content is numeric) I want the RedStyle if it is >= zero then the GreenStyle.

Currently I use the typeof(item) and deal with the type as I know the cell I have assigned the selector to. What I want is to assign the selector to any numeric cell and have the selector do the work without having to reference the underlying object type.

1 Answer, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 13 Jun 2012, 10:25 AM
Hello Mark,

You can try something as follows:

public override Style SelectStyle(object item, DependencyObject container)
        {
            var cell = container as GridViewCell;
            if(cell != null && cell.Value != null)
            {
                if((int)cell.Value > 0)
                {
                    return PositiveStyle;
                }
                else
                {
                    return NegativeStyle;
                }
            }
            return null;
        }

However, this approach has a kind of drawback as initially when SelectStyle is called, the cells might not yet been created. That is why you will need to refresh the grid on a later moment (by calling Rebind() method) - during LayoutUpdated event for example. This might lead to a performance problems in case you have a serious amount of data. 
My recommendation would be to keep up with the approach of casting the item to the required type as in this case you will rely on the underlying data and not on visual elements.
 

Kind regards,
Maya
the Telerik team

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

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