我有以下xaml中的路径数据。我想从后面的代码中定义相同的路径数据。

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

最佳答案

从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;


编辑:

//我们必须设置为true才能将线从lineSegment2绘制到起点

pathFigure.IsClosed = true;

07-28 02:42