Silverlight数据网格发光

时间:2010-01-07 03:00:13

标签: silverlight datagrid glow

我正在尝试一种在数据网格发光中制作单个单元格的方法。

我知道您可以将这样的drophadoweffect应用于整个网格:

 [data:DataGrid.Effect>
  [DropShadowEffect BlurRadius="100" ShadowDepth="0" Color="Red"/>
 [/data:DataGrid.Effect>

但我真正想做的是当值>> = 100时,单个细胞会发红光。从大网格的谷壳中分拣小麦是一种视觉上的好处。

1 个答案:

答案 0 :(得分:2)

我不久前有类似的要求,我解决了如下:

DataGrids当然可以模板化,所以基本上我按照以下方式模板化Columns,然后根据绑定到该单元格的数据绘制了一个转换器,绘制了我的小部件的背景。

<data:DataGridTemplateColumn Header="Overall">
                    <data:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Border Effect="{StaticResource StatusBorderDropShadow}" 
                                Background="{Binding Overall, Converter={StaticResource MiniStatusLightConverter}, Mode=TwoWay}" 
                                Style="{StaticResource StatusBorder}" >                                     
                                <Border.BorderBrush>
                                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                        <GradientStop Color="Black" Offset="0"/>
                                        <GradientStop Color="#FF0D9C3B" Offset="1"/>
                                    </LinearGradientBrush>
                                </Border.BorderBrush>
                            </Border>
                </DataTemplate>
                    </data:DataGridTemplateColumn.CellTemplate>

查看背景绑定如何使用MiniStatusLightConverter。

这是转换器,在您的情况下,您将检查值是否>> 100。

&GT;  命名空间MyProject.Converters {   公共类StatusToBrushConverter:IValueConverter   {     #region IValueConverter的实现

private Dictionary<string, List<Color>> colorsMap;

public StatusToBrushConverter()
{
  colorsMap = new Dictionary<string, List<Color>>
                {
                  { "Green", new List<Color> { GetColorFromHexString("#FFC1D7BF"), GetColorFromHexString("#FF21C110"), GetColorFromHexString("#FF074431") } },
                  { "Yellow", new List<Color> { GetColorFromHexString("#FFF2F3EA"), GetColorFromHexString("#FFD7CB10"), GetColorFromHexString("#FFDEFE00") } },
                  { "Red", new List<Color> { GetColorFromHexString("#FFF3EEEA"), GetColorFromHexString("#FFC13C10"), GetColorFromHexString("#FFFE1000") } }
                };
}

public static Color GetColorFromHexString(string s)
{
  s = s.Remove(0, 1);
  var a = System.Convert.ToByte(s.Substring(0, 2), 16);
  var r = System.Convert.ToByte(s.Substring(2, 2), 16);
  var g = System.Convert.ToByte(s.Substring(4, 2), 16);
  var b = System.Convert.ToByte(s.Substring(6, 2), 16);
  return Color.FromArgb(a, r, g, b);
}

public static string GetColourName(object dtoValue)
{
  try
  {
    var str = dtoValue.ToString();
    if (str.Contains("#"))
    {
      return str.Split('#')[1];
    }
    return str;
  }
  catch (IndexOutOfRangeException)
  {
    return string.Empty;
  }
}

public object Convert(object dtoValue, Type targetType, object parameter, CultureInfo culture)
{
  return new RadialGradientBrush
                      {
                        RadiusX = 0.784, 
                        RadiusY = 0.786, 
                        GradientOrigin = new Point(0.88, 0.115),
                        GradientStops = GetGradientStopCollection(GetColourName(dtoValue)), 
                        RelativeTransform = GetTransformGroupForEffect()
                      };
}

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

#endregion

public object GetBasicSolidColorBrush(object dtoValue)
{
  var blueBrush = new SolidColorBrush();

  if (dtoValue == null)
  {
    blueBrush.Color = Colors.White;
    return blueBrush;
  }

  var value = GetColourName(dtoValue);

  if (value == "Green")
  {
    blueBrush.Color = Colors.Green;
  }
  else if (value == "Yellow")
  {
    blueBrush.Color = Colors.Yellow;
  }
  else if (value == "Red")
  {
    blueBrush.Color = Colors.Red;
  }
  else
  {
    blueBrush.Color = Colors.White;
  }

  return blueBrush;
}

private TransformGroup GetTransformGroupForEffect()
{
  var transform = new TransformGroup();
  transform.Children.Add(new ScaleTransform { CenterX = 0.5, CenterY = 0.5 });
  transform.Children.Add(new SkewTransform { AngleX = 0.0, AngleY = 0.0, CenterX = 0.5, CenterY = 0.5 });
  transform.Children.Add(new RotateTransform { Angle = 4.548, CenterX = 0.5, CenterY = 0.5 });
  return transform;
}

private GradientStopCollection GetGradientStopCollection(string colorValue)
{
  var gradientStopCollection = new GradientStopCollection();

  var stop1 = new GradientStop { Offset = 0.0 };
  var stop2 = new GradientStop { Offset = 0.200 };
  var stop3 = new GradientStop { Offset = 0.858 };

  stop1.Color = colorsMap[colorValue][0];
  stop2.Color = colorsMap[colorValue][1];
  stop3.Color = colorsMap[colorValue][2];

  gradientStopCollection.Add(stop1);
  gradientStopCollection.Add(stop2);
  gradientStopCollection.Add(stop3);

  return gradientStopCollection;
}

} }

我希望至少这会给你带来想法。我尝试的另一种方法是创建一个用户控件并将其放在CellTemplate上,而不仅仅是Border,但我遇到了问题,用户对此功能大吼大叫。它很有用,用户也很开心,这也很重要。

我希望它有所帮助