Apply CellStyleSelector in code behind

3 posts, 1 answers
  1. Stas
    Stas avatar
    5 posts
    Member since:
    Apr 2014

    Posted 21 Mar Link to this post

    I create class for style selector as follows:

    public class NegativeNumberStyle : StyleSelector     {         

        public override Style SelectStyle(object item, DependencyObject container)         {             

             GridViewCell cell = container as GridViewCell;
             var value = item.GetType().GetProperty(cell.Column.UniqueName).GetValue(item, null);
             if (value.GetType() == typeof(int) && ((int)value) < 0) 
    return NegativeStyle;
             if (value.GetType() == typeof(decimal) && ((decimal)value) < 0)  return NegativeStyle;
             
    return null; 
        
    }           

         public Style NegativeStyle { get; set; }
    }

     

    Then I add the resources to the grid on my page as follows:

            <Grid.Resources>
     <my:NegativeNumberStyle x:Key="negativeNumberStyle">
    <my:NegativeNumberStyle.NegativeStyle>
    <Style TargetType="telerik:GridViewCell" BasedOn="StaticResource GridViewCellStyle}">
    <Setter Property="Background" Value="Red"/>
    </Style>
    </my:NegativeNumberStyle.NegativeStyle>
    </my:NegativeNumberStyle>
    </Grid.Recources>

    Since I load columns dynamically I set Columns CellStyle property in code behind

    GridViewDataColumn gridColumn = new GridViewDataColumn();

    gridColumn.CellStyleSelector = new NegativeNumberStyle();

     

    But negative numbers aren't use the proper style (not showing in red color). I debug the code and noticed that NegativeProperty always returns null.

     

    What am I doing wrong?

     

    Thanks

  2. Answer
    Stefan Nenchev
    Admin
    Stefan Nenchev avatar
    281 posts

    Posted 22 Mar Link to this post

    Hi Stas,

    The approach of applying a CellStyleSelector can be reviewed at the following article from our documentation page - Conditionally Apply a Style. As you can see, you need to work with the actual item and not directly with the GridViewCell. Generally, we do not recommend working with the visual elements as this might result in inconsistent behavior due to the UI Virtualization support of the RadGridView.

    Stas, I have attached a sample project that implements the desired behavior for applying a Style for the negative values of a certain column. Please review it and consider such approach at your end.

    Regards,
    Stefan Nenchev
    Telerik
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
  3. UI for WPF is Visual Studio 2017 Ready
  4. Stas
    Stas avatar
    5 posts
    Member since:
    Apr 2014

    Posted 22 Mar Link to this post

    Hi Stephan,

    I was following this article. The only problem is that itc doesn't show how to apply StyleSelector from code behind. Looking in you example I found the problem.

    I replace gridColumn.CellStyleSelector = new NegativeNumberStyle(); with gridColumn.CellStyleSelector = Resources["NegativeNumberStyle"] as StyleSelector; and now it works

     

    Thanks

Back to Top