如何:在 PathGeometry 中创建 LineSegment

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

示例

以下示例绘制了从 (10, 50) 到 (200, 70) 的 LineSegment。 下图显示了已生成的 LineSegment,添加网格背景以显示坐标系统。

A LineSegment in a PathFigure 从 (10,50) 绘制到 (200,70) 的 LineSegment

在 Extensible Application Markup Language (XAML) 中,可以使用属性语法描述路径。

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

(请注意,此属性语法实际上创建了一个 StreamGeometry,即轻量版本的 PathGeometry。有关详细信息,请参阅路径标记语法页面。)

在 XAML 中,还可以使用对象元素语法来绘制线段。 以下内容等效于之前的 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

此示例是更大示例的组成部分;有关完整示例,请参阅几何图形示例

另请参阅