简体   繁体   English

WPF DataGrid - 渐变色标上的彩色单元格

[英]WPF DataGrid - Color Cells on Graded Color Scale

I'm trying to figure out if there is a way to color individual cells on a color scale. 我想弄清楚是否有办法在色标上对单个细胞进行着色。 In particular, I am hoping to get something like the Bonus column in the following: 特别是,我希望在下面得到类似Bonus专栏的内容:

色标示例

Currently, I've setup my datagrid's columns background property to bind to the following converter: 目前,我已将我的datagrid列的后台属性设置为绑定到以下转换器:

        public class NameToBrushConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            double? input = value as double?;
            if(input<-5)
            {
                return Brushes.MediumVioletRed;
            }
            if(-5<=input && input<-0.5)
            { 
                return Brushes.IndianRed;
            }
            if (.5 <= input && input < 5)
            { 
                return Brushes.LightGreen; 
            }
            if (5 <= input)
            { 
                return Brushes.LawnGreen; 
            } 

             return DependencyProperty.UnsetValue;            
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }
}

Is there a way, without hard coding the values, to get a color scale? 有没有一种方法,没有硬编码值,以获得色标?

Thanks - kcross 谢谢 - kcross

EDIT: I'm also trying to create a better Red-Green color scale (ie for negative to positive numbers), in terms of one that is a little less hard on the eyes.... if anyone has any suggestions on that as well that's also appreciated! 编辑:我也试图创建一个更好的红绿色标度(即从负数到正数),就眼睛稍微不那么强的那个......如果有人对此有任何建议,那也很赞赏!

I've created a ValueToBrushConverter. 我创建了一个ValueToBrushConverter。 You use it like this: 你这样使用它:

Background="{Binding Path=YourDoubleValue,
                     Converter={StaticResource ValueToBrushConverter},
                     ConverterParameter='YourMinDouble|YourMaxDouble'}"

This will create a gradient color scale from green ( YourMinDouble ) to red ( YourMaxDouble ) and pick the related color for YourDoubleValue . 这将创建从绿色( YourMinDouble )到红色( YourMaxDouble )的渐变色标,并为YourDoubleValue选择相关颜色。 YourMinDouble can be negativ but has to be lesser then YourMaxDouble . YourMinDouble可以是否定的,但必须YourMaxDouble If YourDoubleValue is not in range it returns Brushes.Transparent . 如果YourDoubleValue不在范围内,则返回Brushes.Transparent
Customize it for your needs! 根据您的需求定制!

ConverterClass ConverterClass

[ValueConversion(typeof(double), typeof(Brush))]
class ValueToBrushConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        double number = (double)value;
        double min = 0;
        double max = 100;

        // Get the value limits from parameter
        try
        {
            string[] limits = (parameter as string).Split(new char[] { '|' });
            min = double.Parse(limits[0], CultureInfo.InvariantCulture);
            max = double.Parse(limits[1], CultureInfo.InvariantCulture);
        }
        catch (Exception)
        {
            throw new ArgumentException("Parameter not valid. Enter in format: 'MinDouble|MaxDouble'");
        }

        if (max <= min)
        {
            throw new ArgumentException("Parameter not valid. MaxDouble has to be greater then MinDouble.");
        }

        if (number >= min && number <= max)
        {
            // Calculate color channels
            double range = (max - min) / 2;
            number -= max - range;
            double factor = 255 / range;
            double red = number < 0 ? number * factor : 255;
            double green = number > 0 ? (range - number) * factor : 255;

            // Create and return brush
            Color color = Color.FromRgb((byte)red, (byte)green, 0);
            SolidColorBrush brush = new SolidColorBrush(color);
            return brush;
        }

        // Fallback brush
        return Brushes.Transparent;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM