在C#的WPF程序使用XAML实现画线
在WPF中画直线。新建WPF应用程序,使用XAML画直线。
使用X1、Y1两个属性可以设置直线的起点坐标,X2、Y2两个属性则可以设置直线的终点坐标。控制起点/终点坐标就可以实现平行、交错等效果。Stroke(笔触)属性的数据类型是Brush(花刷),凡是Brush的派生类均可用于给这个属性赋值。因为WPf提供了多种渐变色花刷,所以画直线也可以画出渐变效果。同时,Line的一些属性还帮助我们画出虚线以及控制线段终点的形状。
下面是例子:
<Window x:Class="FunctionSetting.Views.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="350" Width="525"> <Grid> <Line X1="50" Y1="20" X2="400" Y2="20" Stroke="Red" StrokeThickness="10"></Line> <Line X1="10" Y1="40" X2="260" Y2="40" Stroke="Blue" StrokeThickness="6"></Line> <Line X1="10" Y1="60" X2="260" Y2="20" Stroke="Black" StrokeThickness="6"></Line> <Line X1="20" Y1="60" X2="360" Y2="60" Stroke="Green" StrokeThickness="3"></Line> <Line X1="30" Y1="80" X2="460" Y2="80" Stroke="Purple" StrokeThickness="1"></Line> <Line X1="40" Y1="90" X2="460" Y2="90" Stroke="Black" StrokeThickness="2"></Line> <Line X1="10" Y1="110" X2="360" Y2="110" StrokeDashArray="3" Stroke="RosyBrown" StrokeThickness="6"></Line> <Line X1="10" Y1="120" X2="360" Y2="120" StrokeDashArray="5" Stroke="RosyBrown" StrokeThickness="6"></Line> <Line X1="50" Y1="140" X2="460" Y2="140" StrokeEndLineCap="Flat" Stroke="Brown" StrokeThickness="6"></Line> <Line X1="40" Y1="155" X2="460" Y2="155" StrokeEndLineCap="Triangle" Stroke="Brown" StrokeThickness="8"></Line> <Line X1="200" Y1="10" X2="200" Y2="500" Stroke="Red" StrokeDashArray="10" StrokeThickness="9"></Line> <Line X1="40" Y1="170" X2="460" Y2="170" StrokeEndLineCap="Round" StrokeThickness="8"> <Line.Stroke> <LinearGradientBrush EndPoint="0,0.5" StartPoint="1,0.5"> <GradientStop Color="Blue"/> <GradientStop Offset="1"/> </LinearGradientBrush> </Line.Stroke> </Line> </Grid> </Window>
效果图如下:
道虽迩,不行不至;事虽小,不为不成。