How to dynamically style cells/rows?

3 posts, 1 answers
  1. Todd Davis
    Todd Davis avatar
    36 posts
    Member since:
    Aug 2009

    Posted 08 Apr 2010 Link to this post

    I have a need to occasionally set some styling based on the value of a cell (for example, if the Status cell is "Normal" then leave it alone, but if the Status cell is "Emergency" then turn the text red).

    I attempted to handle this by using the RowLoaded() event and querying the data there, and setting the values, i.e.

    // Get a handle to both the data and the element 
    var data = e.DataElement as WLStudyData; 
    var row = e.Row as GridViewRow; 
     
    if (data == null || row == nullreturn
     
    if (data.Status == Status.Emergency.ToString()) 
        row.Cells[4].Foreground = new SolidColorBrush(Colors.Red); 

    This "sort of" works. The problem is that as I scroll through the records, the Forecolor changes on other items, ones that are NOT marked as Emergency.

    In order to better explain, here is a short video of the problem occurring. Using the code above, only items with a status of StatEmergent should be Red (Stroke is Blue). Look at the top items that are initially black. Watch as I scroll down, then scroll up again. You will see the red text start to "bleed" onto rows where it doesn't belong

    http://screencast.com/t/NGYxMjcyNm

    I was hoping I could perhaps set the styling using the CellStyle property in XAML, however I am not sure how to make that dependent on the cell value?



  2. Answer
    Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 08 Apr 2010 Link to this post

    Hello Todd,

    Please check this blog post for more info about how to achieve this using IValueConverter. Recently we introduced also Style and DataTemplate selectors - you can check my blog post for more info.

    Greetings,
    Vlad
    the Telerik team

    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 Public Issue Tracking system and vote to affect the priority of the items.
  3. DevCraft banner
  4. Todd Davis
    Todd Davis avatar
    36 posts
    Member since:
    Aug 2009

    Posted 08 Apr 2010 Link to this post

    Wow, very powerful feature - that's exactly what I was looking for, thank you.
Back to Top