【问题标题】:Fill ellipse in Silverlight (in the .cs file)在 Silverlight 中填充椭圆(在 .cs 文件中)
【发布时间】:2012-03-08 21:41:24
【问题描述】:

我正在尝试用 Silverlight 中的线性渐变变量填充椭圆(在 .cs 文件中)。这是我尝试过的...

        newEllipse.Fill = ballBG;

但是,这会删除椭圆上已经存在的填充。我也试过...

        newEllipse.Background = ballBG;

但是,它出现了这个错误...“System.Windows.Shapes.Ellipse”不包含“Background”的定义,并且没有扩展方法“Background”接受“System.Windows”类型的第一个参数。可以找到 Shapes.Ellipse'(您是否缺少 using 指令或程序集引用?)

有什么建议吗?

【问题讨论】:

  • “但是,这会移除椭圆上已经存在的填充。” 你期望什么行为?
  • Ellipse 上没有 Background 属性。还有,ballBG是什么类型的?
  • LinearGradientBrush ballBG = new LinearGradientBrush();

标签: c# silverlight


【解决方案1】:

当有 SolidColorBrush 时:

Ellipse redEllipse = new Ellipse();
redEllipse.Height = 100;
redEllipse.Width = 300;           
SolidColorBrush redBrush = new SolidColorBrush();
redBrush.Color = Colors.Red;
SolidColorBrush blackBrush = new SolidColorBrush();
blackBrush.Color = Colors.Black;
redEllipse.StrokeThickness = 4;
redEllipse.Stroke = blackBrush;

redEllipse.Fill = redBrush;

更新 对于您的 LinearGradientBrush,我发现了这个:

LinearGradientBrush myBrush=new LinearGradientBrush ();

myBrush.SetValue(LinearGradientBrush.StartPointProperty, ("0,0"));
myBrush.GradientStops(1).Offset = 0.5;

myBrush.SetValue(LinearGradientBrush.EndPointProperty, ("1,1"));
myBrush.GradientStops(0).Color = Colors.Red;

myBrush.GradientStops(1).Color = Colors.Green;

newEllipse.Fill = myBrush;

【讨论】:

  • 感谢您的建议,无论如何要对 LinearGradientBrush 做同样的事情吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-04
  • 2017-03-13
  • 2011-03-17
  • 1970-01-01
相关资源
最近更新 更多