Adding a path (BezierSegment to a) to the canvas

I'm currently trying to add a BezierSegment to my canvas for my WPF. I get a compile-time error of an invalid cast:

Argument 1: cannot be converted from "System.Windows.Media.PathGeometry" to "System.Windows.UIElement"

This is what I still have ...

 //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); 

Any help would be greatly appreciated!

+4
source share
2 answers

You should add p ( Path ) to the Canvas , not 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 
+5
source

I'm not near a machine that can check it right now, but I think you're almost there. You need to add the System.Windows.Shapes.Path object that you created (you are not currently using it), as well as some parameters to render the line:

  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); 
+2
source

Source: https://habr.com/ru/post/1492509/


All Articles