6

Im new in C# WPF. I want to create a Line in WPF C# with a Point array.

Like:

Point[] points = 
{
  new Point(3,  5),              
  new Point(1 , 40),
  new Point(12, 30),
  new Point(20, 2 )
};

Line myLine = new Line( points );

How can I do this?

  • maybe helpful http://stackoverflow.com/questions/2029680/wpf-c-sharp-path-how-to-get-from-a-string-with-path-data-to-geometry-in-code-n – kenny Aug 22 '14 at 01:27

1 Answers1

9

if you want to draw it with Line, write a method, or you can use Polyline

     public MainWindow()
    {
        InitializeComponent();
        canvas.Children.Clear();
        Point[] points = new Point[4]
        {
            new Point(0,  0),
            new Point(300 , 300),
            new Point(400, 500),
            new Point(700, 100 )
        };
        DrawLine(points);
        //DrawLine2(points);
    }

    private void DrawLine(Point[] points)
    {
        int i;
        int count = points.Length;
        for (i = 0; i < count - 1; i++)
        {
            Line myline = new Line();
            myline.Stroke = Brushes.Red;
            myline.X1 = points[i].X;
            myline.Y1 = points[i].Y;
            myline.X2 = points[i + 1].X;
            myline.Y2 = points[i + 1].Y;
            canvas.Children.Add(myline);
        }
    }

    private void DrawLine2(Point[] points)
    {
        Polyline line = new Polyline();
        PointCollection collection = new PointCollection();
        foreach(Point p in points)
        {
            collection.Add(p);
        }
        line.Points = collection;
        line.Stroke = new SolidColorBrush(Colors.Black);
        line.StrokeThickness = 1;
        canvas.Children.Add(line);
    }
Rang
  • 1,362
  • 7
  • 17
  • 30