views:

87

answers:

2

I have the following below path data which is in xaml. I want to define the same path data from the code behind.

<Path  Data="M 250,40 L200,20 L200,60 Z" />
A: 

You need to use a TypeConverter:

Path path = new Path();
string sData = "M 250,40 L200,20 L200,60 Z";
var converter = TypeDescriptor.GetConverter(typeof(Geometry));
path.Data = (Geometry)converter.ConvertFrom(sData);
Thomas Levesque
The question is about Silverlight, so this does not apply.
Recep
+1  A: 

From Codebehind :

Path orangePath = new Path();

        PathFigure pathFigure = new PathFigure();

        pathFigure.StartPoint = new Point(250, 40);

        LineSegment lineSegment1 = new LineSegment();
        lineSegment1.Point = new Point(200, 20);
        pathFigure.Segments.Add(lineSegment1);

        LineSegment lineSegment2 = new LineSegment();
        lineSegment2.Point = new Point(200, 60);
        pathFigure.Segments.Add(lineSegment2);

        PathGeometry pathGeometry = new PathGeometry();
        pathGeometry.Figures = new PathFigureCollection();

        pathGeometry.Figures.Add(pathFigure);

        orangePath.Data = pathGeometry;

Edit :

//we should have to set this true to draw the line from lineSegment2 to the start point

pathFigure.IsClosed = true;
Malcolm