Hi I need to implement a function that if the value of the binding items is within the specific range cell color should be according to the range.
I have been using Changing Background Color Of DataGrid Cell WPF 4
this works fine but it is for only if that values are there.what if i want to add range i.e from 10 – 20 it is red 21-30 it is blue
added everything and saw an example at the end but the color does not change here is the code
Class
public class ConvertToBrush : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
int tempValue = int.Parse(value.ToString());
string tempString = "Red";
if (tempValue >= 0 && tempValue <= 20)
tempString = "#FF0000";
if (tempValue > 20 && tempValue <= 40)
tempString = "#F09300";
if (tempValue > 40 && tempValue <= 60)
tempString = "#EDDF00";
if (tempValue > 60 && tempValue <= 80)
tempString = "#FFFFFF";
if (tempValue > 80 && tempValue <= 100)
tempString = "#85AB00";
SolidColorBrush brush = new SolidColorBrush();
BrushConverter conv = new BrushConverter();
brush = conv.ConvertFromString(tempString) as SolidColorBrush;
return brush;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return DependencyProperty.UnsetValue;
}
}
XMAL
<DataGridTextColumn ElementStyle="{StaticResource CentreAlignStyle}" Binding="{Binding TestResults}" Header="Results" IsReadOnly="True" MaxWidth="60" MinWidth="60" >
<DataGridTextColumn.CellStyle>
<Style>
<Setter Property="TextBlock.Background" Value="{Binding TestResults, Converter={StaticResource makeBrush}}" />
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>
Don’t use a
DataTriggerbut just bind theBackgroundto the value and put in aValueConverterto return the right brush (or no brush at all).Edit: What the usage should look like: