Polyline In WPF

A polyline is a collection of connected straight lines. The Polyline object represents a polyline shape and draws a polyline with the given points. The Points property represents the points in a polyline. The Stroke property sets the color and StrokeThickness represents the width of the line of a polyline.

Creating a Polyline

The Polyline element in XAML creates a polyline shape. The following code snippet creates a polyline by setting its Points property. The code also sets the black stroke of width 4. 

  1. <Polyline Points="10,100 100,200 200,30 250,200 200,150" Stroke="Black" StrokeThickness="4" />  

The output looks like Figure 12.

A Polyline 
Figure 12. A Polyline

The CreateAPolyline method listed in Listing 11 draws the same rectangle in Figure 12 dynamically.

  1. privatevoid CreateAPolyline()  
  2. {  
  3.     // Create a blue and a black Brush  
  4.     SolidColorBrush yellowBrush = newSolidColorBrush();  
  5.     yellowBrush.Color = Colors.Yellow;  
  6.     SolidColorBrush blackBrush = newSolidColorBrush();  
  7.     blackBrush.Color = Colors.Black;  
  8.     // Create a polyline  
  9.     Polyline yellowPolyline = newPolyline();  
  10.     yellowPolyline.Stroke = blackBrush;  
  11.     yellowPolyline.StrokeThickness = 4;  
  12.     // Create a collection of points for a polyline  
  13.     System.Windows.Point Point1 = new System.Windows.Point(10, 100);  
  14.     System.Windows.Point Point2 = new System.Windows.Point(100, 200);  
  15.     System.Windows.Point Point3 = new System.Windows.Point(200, 30);  
  16.     System.Windows.Point Point4 = new System.Windows.Point(250, 200);  
  17.     System.Windows.Point Point5 = new System.Windows.Point(200, 150);  
  18.     PointCollection polygonPoints = newPointCollection();  
  19.     polygonPoints.Add(Point1);  
  20.     polygonPoints.Add(Point2);  
  21.     polygonPoints.Add(Point3);  
  22.     polygonPoints.Add(Point4);  
  23.     polygonPoints.Add(Point5);  
  24.     // Set Polyline.Points properties  
  25.     yellowPolyline.Points = polygonPoints;  
  26.     // Add polyline to the page  
  27.     LayoutRoot.Children.Add(yellowPolyline);  
  28. }  

Listing 11


Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.