Colorpleth值转换器

时间:2014-01-15 07:06:36

标签: wpf colors ivalueconverter

我需要在WPF应用程序上显示与项目属性值的测量成比例的数据,如下图所示。

enter image description here

我的问题是如何根据相对值提供颜色/ LinearGradientBrush(绿 - 黑 - 红)?最低值应返回红色(碟形),中间值为黑色/灰色(ish),最高值为greed(ish),如图像所示。

我开始使用转换器将每个面板的背景颜色绑定到项目的值,该转换器可以返回一些单独的颜色,但我想根据相对值返回所有可能的LinearGradientBrushes。

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

1 个答案:

答案 0 :(得分:0)

查看类似问题的this答案。似乎做你想做的事。