Change the Color of GridViewCell Based on a Property's Value

Environment

Product Version 2019.3 917
Product RadGridView for WPF

Description

How to change the color of a GridViewCell based on the value of a property of the underlying data item.

Solution

To achieve the desired result, you need to create a converter with multiple conditions.

public class ValueToColorConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
        int count = (int)value; 
        if (count < 0 && count >=-2) 
            return new SolidColorBrush(Colors.Orange); 
        else if (count < -2) 
            return new SolidColorBrush(Colors.Red); 
        else if (count > 0 && count <=2) 
            return new SolidColorBrush(Colors.GreenYellow); 
        else if (count > 2) 
            return new SolidColorBrush(Colors.Green); 
        return new SolidColorBrush(Colors.White); 
    } 
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
        throw new NotImplementedException(); 
    } 
} 

<Grid.Resources> 
    <local:ValueToColorConverter x:Key="ColorConverter"/> 
<!-- If you are using the StyleManager theming mechanism with the XAML binaries, remove the BasedOn attributes --> 
    <Style TargetType="telerik:GridViewCell" BasedOn="{StaticResource GridViewCellStyle}"> 
        <Setter Property="Background" Value="{Binding Value, Converter={StaticResource ColorConverter}}"/> 
    </Style> 
</Grid.Resources> 
In this article
Not finding the help you need? Improve this article