如何:在 PathGeometry 中创建 LineSegment

更新:2007 年 11 月

本示例演示如何创建线段。若要创建线段,请使用 PathGeometryPathFigureLineSegment 类。

示例

下面的示例绘制一个从 (10, 50) 到 (200, 70) 的 LineSegment。下图显示了最后生成的 LineSegment;为了显示坐标系,添加了网格背景。

从 (10,50) 绘制到 (200,700) 的 LineSegment

PathFigure 中的一个 LineSegmentxaml

在可扩展应用程序标记语言 (XAML) 中,可以使用属性语法来描述路径。

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

xaml

(请注意,此属性语法实际上创建一个 StreamGeometry,这是 PathGeometry 的轻量版本。有关更多信息,请参见路径标记语法页。)

在 XAML 中,还可以使用对象元素语法来绘制线段。下面的示例与前面的 XAML 示例是等效的。

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>

此示例摘自一个更大的示例;有关完整的示例,请参见几何图形示例

请参见

概念

Geometry 概述

参考

PathFigure

PathGeometry

GeometryDrawing

Path