此示例演示如何创建线段。 若要创建线段,请使用 PathGeometry、PathFigure和 LineSegment 类。
示例:
以下示例将 LineSegment 从 (10, 50) 改为 (200, 70)。 下图显示了生成的 LineSegment;添加了网格背景以显示坐标系。
从 (10,50) 绘制到 (200,70) 的 LineSegment
在可扩展应用程序标记语言(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
此示例是较大示例的一部分;有关完整示例,请参阅 几何图形示例。