Procedura: creare un oggetto LineSegment in un oggetto PathGeometry

Aggiornamento: novembre 2007

In questo esempio viene illustrato come creare un segmento di linea. Per creare un segmento di linea, utilizzare le classi PathGeometry, PathFigure e LineSegment.

Esempio

Negli esempi riportati di seguito viene disegnato LineSegment da (10, 50) a (200, 70). Nella figura seguente viene illustrato l'oggetto LineSegment risultante. È stato aggiunto uno sfondo a griglia per visualizzare il sistema di coordinate.

Oggetto LineSegment disegnato da (10,50) a (200,700)

LineSegment in PathFigurexaml

In Extensible Application Markup Language (XAML), è possibile utilizzare la sintassi di attributo per descrivere un percorso.

<Path Stroke="Black" StrokeThickness="1"  
  Data="M 10,50 L 200,70" />

xaml

Si noti che la sintassi di attributo crea in effetti un oggetto StreamGeometry, una versione leggera di PathGeometry. Per ulteriori informazioni, vedere la pagina Sintassi di markup del percorso.

In XAML, è possibile disegnare un segmento di linea anche utilizzando la sintassi di elemento oggetto. L'esempio seguente equivale all'esempio di XAML precedente.

PathFigure myPathFigure = new PathFigure();
myPathFigure.StartPoint = new Point(10, 50);

LineSegment myLineSegment = new LineSegment();
myLineSegment.Point = new Point(200, 70);

PathSegmentCollection myPathSegmentCollection = new PathSegmentCollection();
myPathSegmentCollection.Add(myLineSegment);

myPathFigure.Segments = myPathSegmentCollection;

PathFigureCollection myPathFigureCollection = new PathFigureCollection();
myPathFigureCollection.Add(myPathFigure);

PathGeometry myPathGeometry = new PathGeometry();
myPathGeometry.Figures = myPathFigureCollection;

Path myPath = new Path();
myPath.Stroke = Brushes.Black;
myPath.StrokeThickness = 1;
myPath.Data = myPathGeometry;
<Path Stroke="Black" StrokeThickness="1">
  <Path.Data>
    <PathGeometry>
      <PathFigure StartPoint="10,50">
        <LineSegment Point="200,70" />
      </PathFigure>
    </PathGeometry>
  </Path.Data>
</Path>

Per la versione completa dell'esempio di cui fa parte questo esempio, vedere Esempio di geometrie.

Vedere anche

Concetti

Cenni preliminari sulle classi Geometry

Riferimenti

PathFigure

PathGeometry

GeometryDrawing

Path