HOW TO:在 PathGeometry 中建立 LineSegment
更新:2007 年 11 月
本範例示範如何建立線段。若要建立線段,請使用 PathGeometry、PathFigure 和 LineSegment 類別。
範例
下列範例會繪製一條從 (10, 50) 到 (200, 70) 繪製 LineSegment。下圖顯示產生的 LineSegment;加入格線背景是為了顯示座標系統。
從 (10,50) 繪製至 (200,700) 的 LineSegment
xaml
在可延伸標記語言 (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>
本範例是完整範例的一部分;如需完整範例,請參閱幾何範例。