操作說明:在 PathGeometry 中建立 LineSegment
這個範例會示範如何建立線段 。 若要建立線段,請使用 PathGeometry、PathFigure 和 LineSegment 類別。
範例
下列範例會將 LineSegment 從 (10, 50) 繪製到 (200, 70)。 下圖顯示所產生的 LineSegment;已新增格線背景以顯示座標系統。
從 (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
這個範例屬於較大型的範例;如需完整範例,請參閱幾何範例。