Compartir vía


Cómo: Crear un segmento de línea en una clase PathGeometry

En este ejemplo se muestra cómo crear un segmento de línea. Para crear un segmento de línea, utilice las clases PathGeometry, PathFigure y LineSegment.

Ejemplo

En los ejemplos siguientes se dibuja un elemento LineSegment de (10, 50) a (200, 70). En la ilustración siguiente se muestra el resultado de LineSegment; se ha agregado un fondo de cuadrícula para mostrar el sistema de coordenadas.

Segmento de línea en un elemento PathFigure Segmento de línea dibujado de (10,50) a (200,70)

En el lenguaje XAML, se puede utilizar la sintaxis de atributo para describir un trazado.

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

Tenga en cuenta que esta sintaxis de atributo crea realmente un elemento StreamGeometry versión más ligera de PathGeometry. Para obtener más información, consulte la página Sintaxis de marcado de trazados.

En XAML, también puede dibujar un segmento de línea con la sintaxis de elemento de objeto. El siguiente ejemplo es equivalente al ejemplo anterior de XAML.

<Path Stroke="Black" StrokeThickness="1">
  <Path.Data>
    <PathGeometry>
      <PathFigure StartPoint="10,50">
        <LineSegment Point="200,70" />
      </PathFigure>
    </PathGeometry>
  </Path.Data>
</Path>
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;
Dim myPathFigure As New PathFigure()
myPathFigure.StartPoint = New Point(10, 50)

Dim myLineSegment As New LineSegment()
myLineSegment.Point = New Point(200, 70)

Dim myPathSegmentCollection As New PathSegmentCollection()
myPathSegmentCollection.Add(myLineSegment)

myPathFigure.Segments = myPathSegmentCollection

Dim myPathFigureCollection As New PathFigureCollection()
myPathFigureCollection.Add(myPathFigure)

Dim myPathGeometry As New PathGeometry()
myPathGeometry.Figures = myPathFigureCollection

Dim myPath As New Path()
myPath.Stroke = Brushes.Black
myPath.StrokeThickness = 1
myPath.Data = myPathGeometry

Este ejemplo forma parte de un ejemplo más extenso; para obtener el ejemplo completo, vea Ejemplo de geometrías.

Vea también