2012-04-18 130 views
3

我試圖弄清楚是否有方法在顏色標尺上對單個單元格着色。特別是,我希望能得到類似下面的獎金列:WPF DataGrid - 分級色標上的顏色單元格

Color scale example

目前,我設置我的DataGrid的列background屬性綁定到下面的轉換:

 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(); 
     } 
    } 
} 

有沒有辦法,沒有硬編碼的價值,得到一個色階?

謝謝 - kcross

編輯:我也想創造一個更好的紅 - 綠色標(即負到正數),在一個就是有點不太硬的眼睛的條款。 ...如果任何人有任何建議,以及也讚賞!

回答

4

我創建了一個ValueToBrushConverter。您可以使用這樣的:

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

這將從綠色(YourMinDouble)創建漸變色標爲紅色(YourMaxDouble),並選擇相關的顏色YourDoubleValueYourMinDouble可能是負面的,但必須小於YourMaxDouble。如果YourDoubleValue不在範圍內,則返回Brushes.Transparent
根據您的需求定製它!

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(); 
    } 
} 
+0

真棒,感謝您的!非常感激 – keynesiancross 2012-04-22 21:44:38