Silverlight 数据网格发光

发布于 2024-08-16 17:14:57 字数 297 浏览 9 评论 0原文

我正在尝试想出一种方法来使数据网格中的单个单元格发光。

我知道你可以将这样的 dropshadoweffect 应用到整个网格:

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

但我真正想做的是当值 >= 到 100 时让单个单元格发出红色光。对小麦进行排序有点视觉效果来自大网格的谷壳。

I'm trying to think of a way to make an individual cell within a datagrid glow.

I know that you can apply a dropshadoweffect like this to the whole grid:

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

but what I really want to do is have the individual cell glow red when the value is >= to 100. It's a bit of a visual niceness to sort the wheat from the chaff of a large grid.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花开雨落又逢春i 2024-08-23 17:14:57

不久前我有一个类似的需求,我解决如下:

DataGrid当然可以模板化,所以基本上我如下模板化列,然后有一个转换器根据绑定到该单元格的数据绘制我的小部件的背景。

<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。

>
命名空间 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 上而不仅仅是边框上,但我遇到了问题,用户对我的此功能大喊大叫。它有效并且用户很高兴,这也很重要。

我希望它有帮助

I had a similar requirement not long ago which I solved as follows:

DataGrids can be templated of course, so basically I templated the Columns as follows and then had a Converter that painted the background of my widget according to the data bound to this cell.

<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>

Have a look at the background binding how it uses the MiniStatusLightConverter.

Here is the converter, in your case you will check if the value is >= 100.

>
namespace MyProject.Converters
{
public class StatusToBrushConverter : IValueConverter
{
#region Implementation of 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;
}

}
}

I hope at least this brings you ideas. Another approach I tried was create an user control and put it on the CellTemplate rather than just the Border but I had issues and users were screaming at me for this functionality. It works and users are happy, that counts too.

I hope it helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文