将(BezierSegment添加到画布)路径

本文关键字:路径 BezierSegment 添加 | 更新日期: 2023-09-27 18:24:00

我目前正试图将BezierSegment添加到我的WPF画布中。我得到了一个不正确的转换的编译时错误:

参数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);

如有任何帮助,我们将不胜感激!

将(BezierSegment添加到画布)路径

必须将pPath)添加到Canvas,而不是pathPathGeometry

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

我现在离一台可以检查的机器不远,但我想你已经快到了。您需要添加您创建的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);