Compartir a través de


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

Actualización: noviembre 2007

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

Ejemplo

En los ejemplos siguientes se dibuja un objeto LineSegment desde (10, 50) hasta (200, 70). En la ilustración siguiente se muestra el objeto LineSegment resultante; se agregó un fondo de cuadrícula para mostrar el sistema de coordenadas.

Objeto LineSegment dibujado de (10,50) a (200,700)
LineSegment en PathFigurexaml

En Lenguaje de marcado de aplicaciones extensible (XAML), puede utilizar la sintaxis de atributo para describir un trazado.

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

xaml

(Tenga en cuenta que, en realidad, esta sintaxis de atributo crea un objeto StreamGeometry, que es una versión ligera de PathGeometry. Para obtener más información, vea la página Sintaxis de marcado de trazados.)

En XAML, también puede dibujar un segmento de línea utilizando la sintaxis de elementos de objeto. El código siguiente equivale al ejemplo de XAML anterior.

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>

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

Vea también

Conceptos

Información general sobre geometría

Referencia

PathFigure

PathGeometry

GeometryDrawing

Path