添加(BezierSegment到a)画布路径

时间:2013-07-20 20:31:49

标签: c# wpf drawing bezier

我目前正在尝试为我的WPF添加BezierSegment到我的画布。我收到了一个不正确演员的编译时错误:

  

参数1:无法从'System.Windows.Media.PathGeometry'转换为'System.Windows.UIElement'

这就是我到目前为止......

//bezier curve it 
BezierSegment curve = new BezierSegment(startPoint, endPoint,controlPoint,false);

// Set up the Path to insert the segments
PathGeometry path = new PathGeometry();

PathFigure pathFigure = new PathFigure();
pathFigure.StartPoint = hs.LeStartingPoint;
pathFigure.IsClosed = true;
path.Figures.Add(pathFigure);

pathFigure.Segments.Add(curve);
System.Windows.Shapes.Path p = new Path();
p.Data = path;
this.mainWindow.MyCanvas.Children.Add(path);

任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:5)

您必须将p Path )添加到Canvas,而不是path PathGeometry )。

BezierSegment curve = new BezierSegment(new Point(11,11), new Point(22,22), new Point(15,15), false);           

// Set up the Path to insert the segments
PathGeometry path = new PathGeometry();

PathFigure pathFigure = new PathFigure();
pathFigure.StartPoint = new Point(11, 11);
pathFigure.IsClosed = true;
path.Figures.Add(pathFigure);

pathFigure.Segments.Add(curve);
System.Windows.Shapes.Path p = new Path();
p.Stroke = Brushes.Red;
p.Data = path;

MyCanvas.Children.Add(p); // Here

答案 1 :(得分:2)

我不在能够立即检查它的机器附近,但我认为你几乎就在那里。您需要添加您创建的System.Windows.Shapes.Path对象(您当前没有使用它),以及一些参数以使该行实际渲染:

 System.Windows.Shapes.Path p = new Path();
 p.Data = path;
 p.Fill = System.Windows.Media.Brushes.Green;
 p.Stroke = System.Windows.Media.Brushes.Blue;
 p.StrokeThickness = 1;

 this.MyCanvas.Children.Add(p);