共用方式為


HOW TO:在 PathGeometry 中建立 LineSegment

本範例示範如何建立線段。 若要建立線段,請使用 PathGeometryPathFigureLineSegment 類別。

範例

下列範例會繪製一條從 (10, 50) 到 (200, 70) 繪製 LineSegment。 下圖顯示產生的 LineSegment;加入格線背景是為了顯示座標系統。

從 (10,50) 繪製至 (200,700) 的 LineSegment

PathFigure 中的 LineSegment

[xaml]

在Extensible Application Markup Language (XAML) 中,您可以使用屬性語法來描述路徑。

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

[xaml]

(請注意,此屬性語法實際上會建立 StreamGeometry,這是 PathGeometry 的輕量版。 如需詳細資訊,請參閱路徑標記語法網頁)。

在 XAML 中,您也可以使用物件項目語法繪製線段。 以下內容相當於前一個 XAML 範例。

            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
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>

這個範例是完整範例的一部分。如需完整範例,請參閱幾何範例 (英文)。

請參閱

參考

PathFigure

PathGeometry

GeometryDrawing

Path

概念

幾何概觀