I'm currently trying to add a BezierSegment
to my canvas for my WPF. I'm getting a compile time error of an improper cast:
Argument 1: cannot convert from 'System.Windows.Media.PathGeometry' to 'System.Windows.UIElement'
This is what I have so far...
//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!
You must add p
(Path
) to 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
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 aren't currently using it), along with some parameters to make the line actually render:
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With