0

I need to display data on a WPF application colored in proportion to the measurement of the item's property value, like on the image below.

enter image description here

My question is how to provide the color/LinearGradientBrush (green-black-red) based on the relative value? Lowest values should return red(dish), middle values black/gray(ish) and highest values greed(ish), as in the image.

I started binding the background color of each panel to the item's value with a converter that can return some individual colors but I'd like to return a full range of possible LinearGradientBrushes based on the relative value.

Public Class ValueToColorConverter
    Inherits MarkupExtension
    Implements IValueConverter

    Public Function Convert(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.Convert
        Dim v As Double = System.Convert.ToDecimal(value)
        If v < 0 Then
            Return Brushes.Red
        ElseIf v < 0.05 Then
            Return Brushes.Gray
        Else
            Return Brushes.Green
        End If
    End Function

    Public Function ConvertBack(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
        Throw New System.NotImplementedException()
    End Function

    Public Overrides Function ProvideValue(ByVal serviceProvider As System.IServiceProvider) As Object
        Return Me
    End Function

End Class
Sheridan
  • 68,826
  • 24
  • 143
  • 183
Nuts
  • 2,755
  • 6
  • 33
  • 78
  • I guess LinearGradientBrush is not what you need here. Sounds like you just want to create a `SolidColorBrush` from a specific color value. Take a look at the [`Color.FromArgb`](http://msdn.microsoft.com/en-us/library/system.windows.media.color.fromargb.aspx) method. – Clemens Jan 15 '14 at 08:13
  • What's wrong with the current solution? That seems to be the way that I would do it. – Sheridan Jan 15 '14 at 09:09
  • The problem with the current solution is that it can return only 3 unique colors (Red, Gray or Green) but I want to return a color from a larger range (unlimited number) of possible colors. I want to calculate a specific color (SolidColor or LinearGradient, anything, anything goes) from the given value. – Nuts Jan 23 '14 at 20:53

0 Answers0