mike
mike

Reputation:

Need help to convert XAML into C# code

this is a piece of XAML script that draw a Bezier code. What I need is a pure C# code behind that achieve the same result but not using XAML.

Can anybody help convert it into C# code?

Thanks in advance!

Mike

<Path Stroke="Black" StrokeThickness="1">
  <Path.Data>
    <PathGeometry>
      <PathGeometry.Figures>
        <PathFigureCollection>
          <PathFigure StartPoint="10,100">
            <PathFigure.Segments>
              <PathSegmentCollection>
                <BezierSegment Point1="100,0" Point2="200,200" Point3="300,100" />
              </PathSegmentCollection>
            </PathFigure.Segments>
          </PathFigure>
        </PathFigureCollection>
      </PathGeometry.Figures>
    </PathGeometry>
  </Path.Data>
</Path>

Upvotes: 3

Views: 1665

Answers (2)

subho
subho

Reputation: 59

Path path = new Path();
path.Stroke = Brushes.Black;
path.StrokeThickness = 1;
PathGeometry pg = new PathGeometry();
PathFigureCollection pfc = new PathFigureCollection();
PathFigure fig = new PathFigure();
fig.StartPoint = new Point(10,100);
PathSegmentCollection psc = new PathSegmentCollection();
BezierSegment bs1 = new BezierSegment(new Point(100, 0), new Point(200, 200), new Point(300, 100), true);
psc.Add(bs1);
fig.Segments = psc;
pfc.Add(fig);
pg.Figures = pfc;
path.Data = pg;
canvas.Children.Add(path);

Upvotes: 0

John
John

Reputation: 337

I tested this code and it works.

        Path path = new Path();
        path.Stroke = new SolidColorBrush(Colors.Black);
        path.StrokeThickness = 10;
        PathGeometry pg = new PathGeometry();
        PathFigureCollection pfc = new PathFigureCollection();
        PathFigure fig = new PathFigure();
        PathSegmentCollection psc = new PathSegmentCollection();
        BezierSegment bs1 = new BezierSegment(new Point(100, 0), new Point(200, 200), new Point(300, 100), true);
        psc.Add(bs1);
        fig.Segments = psc;
        pfc.Add(fig);
        pg.Figures = pfc;
        path.Data = pg;

Upvotes: 4

Related Questions