Path最重要的属性是Data,Data的数据类型是Geometry(是抽象类,要用派生类的标签),赋值方法有两种,一种是标签式标准语法,第二种是路径标记语法。
基础知识
Geometry的子类有:
LineGeometry:直线几何图形
RectangleGeometry:矩形几何图形
EllipseGeometry
PathGeometry
StreamGeometry:PathGeometry的轻量级替代品,不支持Binding、动画等
CombinedGeometry:由多个基本几何图形联合在一起,形成的单一几何图形
GeometryGroup:这个是一组,不是单一图形
前面的Line/Rectangle/Ellipse类都是可以独立存在的对象,而LineGeometry这些只能用于结合成其他几何图形,不能独立存在。当在Blend中选一组独立的几何图形并在菜单里执行组合路径的命令时,本质是把原本独立的Line/Rectangle/Ellipse变成了*Geometry对象,然后组合成一个新的复杂图形。
绘制<PathFigure.Segments>时,这些线段都没有起点,因为起点就是前一个线段的终点。第一个线段的起点是PathFigure的StartPoint。
Path实例:标签定义
<UserControl x:Class="pxy.PathLabelBasic"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:pxy"
mc:Ignorable="d"
d:DesignHeight="400" d:DesignWidth="400">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="160"/>
<ColumnDefinition Width="160"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="160"/>
<RowDefinition Height="240"/>
</Grid.RowDefinitions>
<!--直线-->
<Path Stroke="CornflowerBlue" StrokeThickness="2" Grid.Column="0" Grid.Row="0">
<Path.Data>
<LineGeometry StartPoint="20,20" EndPoint="140,140"/>
</Path.Data>
</Path>
<!--矩形-->
<Path Stroke="Orange" Fill="Yellow" Grid.Column="1" Grid.Row="0">
<Path.Data>
<RectangleGeometry Rect="20,20,120,120" RadiusX="10" RadiusY="10"/>
</Path.Data>
</Path>
<!--椭圆-->
<Path Stroke="Green" Fill="LawnGreen" Grid.Column="0" Grid.Row="1">
<Path.Data>
<EllipseGeometry Center="80,80" RadiusX="60" RadiusY="40"/>
</Path.Data>
</Path>
<!--自定义路径-->
<Path Stroke="Yellow" Fill="Orange" Grid.Column="1" Grid.Row="1">
<Path.Data>
<PathGeometry>
<PathGeometry.Figures> <!--Figures是PathGeometry的默认内容属性,可省略-->
<PathFigure StartPoint="25,140" IsClosed="True">
<PathFigure.Segments> <!--Segments也是默认内容属性,也可以省略-->
<LineSegment Point="20,40"/>
<LineSegment Point="40,110"/>
<ArcSegment Point="110,150" Size="80,80"/> <!--连接终点-->
</PathFigure.Segments>
</PathFigure>
</PathGeometry.Figures>
</PathGeometry>
</Path.Data>
</Path>
</Grid>
</UserControl>
说明:
ArcSegment的Point属性用来指明圆弧连接的终点;圆弧截取自椭圆,Size指定的是横轴半径和纵轴半径;SweepDirection指的是圆弧是按顺时针方向还是逆时针方向。如果椭圆上两点不对称,那两点间的圆弧可分为大弧和小弧,IsLargeArc可以用来指明是否用大弧连接。RotationAngle指明圆弧母椭圆的旋转角度。
举例(用到了后面的路径标记语法)
<Grid>
<Path Stroke="Black" Fill="Bisque" Data="M 10,10 A 180,100 30 0 1 150,150"/>
</Grid>
说明:M 10,10 指从头开始,A表示开始画圆弧,(180,100)是椭圆的横轴半径和纵轴半径,30指椭圆的旋转角度(椭圆中心与原点连线,与y正半轴的夹角),0指小弧,1指圆弧是顺时针,(150,150)是线段终点。
<Path Stroke="Black" Fill="Bisque" Data="M 10,10 A 180,100 30 1 1 150,150"/>
贝塞尔曲线BezierSegment
起点:前一个线段的终点/PathFigure的StartPoint
终点:Point3属性
两个控制点:Point1和Point2
三次方贝塞尔曲线就是从起点走向Point1,再走向Point2方向,最后到达终点的平滑曲线。
二次方贝塞尔曲线QuadraticBezierSegment
控制点是Point1,终点Point2
<QuadraticBezierSegment Point1="150,-100" Point2="160,200"/>
GeometryGroup
在< Path><Path.Data>下可以放< GeometryGroup>,其下可以放几个< PathGeometry>