Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding (BezierSegment to a) Path to Canvas

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!

like image 548
Ace Avatar asked Jul 20 '13 20:07

Ace


2 Answers

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
like image 161
Anatoliy Nikolaev Avatar answered Nov 09 '22 08:11

Anatoliy Nikolaev


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);
like image 2
Chris Avatar answered Nov 09 '22 08:11

Chris