WPF GridView Change Cell Color Based on Value

5 posts, 0 answers
  1. Sagar
    Sagar avatar
    1 posts
    Member since:
    Dec 2013

    Posted 11 Dec 2013 Link to this post

     i have one GridView that contains Column Name "Day1", "Day2" ....to "Day31".

    Now i want to change Cell Background and ForeColor Based On Value.

    For All the 31 Columns
    if cell value = "P" then Green  Background and forecolor white.

    how can i do this.
    Please Help me.
  2. Dimitrina
    Dimitrina avatar
    3769 posts

    Posted 11 Dec 2013 Link to this post


    You can check our online documentation on how to use a CellStyleSelector here. The "Style Selectors -> Cells" WPF demo would also be helpful for you.

    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
  3. Renier Pretorius
    Renier Pretorius avatar
    55 posts
    Member since:
    Apr 2010

    Posted 01 Jun 2015 in reply to Dimitrina Link to this post


     I have taken a look at the documentation and the demos (Silverlight) but in all cases your CellStyleSelector is based on a business object with a specific property in the column to which the style needs to be applied. In my case my business object as an array of values which I want to display using the same CellStyleSelector. I build the columns in the code behind. So to use your method my CellStyleSelector must be aware of the specific column in question. Any pointers?




  4. Dimitrina
    Dimitrina avatar
    3769 posts

    Posted 02 Jun 2015 Link to this post

    Hi Renier,

    You can find the column associated with the cell passed as the container argument. The code to access it would be similar to this one:
    GridViewCell cell = container as GridViewCell;
    var currentColumn = cell.Column as GridViewDataColumn;

    My suggestion is to work with the bound data item and CellStyleSelection and not with the visual elements directly. As RadGridView is a virtualized control, its elements are reused as they are brought in and out the view area. You can also check our online documentation explaining how the UI virtualization works. 

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  5. Tom
    Tom avatar
    7 posts
    Member since:
    May 2016

    Posted 09 Aug 2016 in reply to Dimitrina Link to this post

    I found an easier approach using a single ValueConverter (not aware of the DataContext)

    I think this is usually better than the heavy StyleSelector approach, you can also reuse the ValueConverter for different columns more easily.


    Set the GridColumns CellStyle to

    <Style x:Key="IsSentCellStyle" TargetType="telerik:GridViewCell" BasedOn="{StaticResource GridViewCellStyle}">
                <Setter Property="Background"
                        Value="{Binding IsSent, Converter={controls:FalseToRedBrushConverter}}"/>

Back to Top