在 WPF 中的一行的开头和结尾放置一个形状
Put a shape at the start and end of a line in WPF
我在我的应用程序中使用 System.Windows.Shapes.Line 在 canvas 上画一条线。我想放一个形状,例如行首和行尾的十字('x')?有没有办法通过设置 属性 来做到这一点。我可以根据坐标在 canvas 上添加一个 'x',但我希望我们是否可以直接使用一些 Line 属性 来做到这一点。
目前我能画出这个 = --------------
dashed line 使用以下代码段中的属性:-
var DistanceLine = new Line();
DistanceLine.Stroke = new SolidColorBrush(LineColor);
DistanceLine.StrokeDashArray = new DoubleCollection() {0, 4};
DistanceLine.StrokeDashCap = PenLineCap.Round;
DistanceLine.StrokeEndLineCap = PenLineCap.Round;
DistanceLine.StrokeLineJoin = PenLineJoin.Round;
DistanceLine.StrokeStartLineCap = PenLineCap.Round;
DistanceLine.StrokeThickness = 3;
我想要这样的东西 = x----------------x
dashed line with 'x' marks
如何在线条的末尾制作自定义形状?
评论建议并鼓励我写下自己的形状放在线帽处。这可能不是最好的方法,但对我来说效果很好。 class returns 一个作为网格的对象,当我画一条线时,我可以将其添加到我的 canvas 中。我是这样做的:-
public class CrossHair : Grid
{
public string LineName { get; set; }
/// <summary>
/// Draws the crosshair at the given point
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
static public CrossHair DrawCrossHair(double x, double y)
{
var crosshair = new CrossHair(); // to contain the cross hair
var line1 = new Line();
var line2 = new Line();
var line3 = new Line();
var line4 = new Line();
line1.Stroke = line2.Stroke = line3.Stroke = line4.Stroke = new SolidColorBrush(Colors.Yellow);
line1.StrokeThickness = line2.StrokeThickness = line3.StrokeThickness = line4.StrokeThickness = 2;
line1.X1 = x - 5;
line1.Y1 = y;
line1.X2 = x - 2;
line1.Y2 = y;
line2.X1 = x;
line2.Y1 = y + 5;
line2.X2 = x;
line2.Y2 = y + 2;
line3.X1 = x + 2;
line3.Y1 = y;
line3.X2 = x + 5;
line3.Y2 = y;
line4.X1 = x;
line4.Y1 = y - 2;
line4.X2 = x;
line4.Y2 = y - 5;
crosshair.Children.Add(line1);
crosshair.Children.Add(line2);
crosshair.Children.Add(line3);
crosshair.Children.Add(line4);
return crosshair;
}
}
我在我的应用程序中使用 System.Windows.Shapes.Line 在 canvas 上画一条线。我想放一个形状,例如行首和行尾的十字('x')?有没有办法通过设置 属性 来做到这一点。我可以根据坐标在 canvas 上添加一个 'x',但我希望我们是否可以直接使用一些 Line 属性 来做到这一点。 目前我能画出这个 = -------------- dashed line 使用以下代码段中的属性:-
var DistanceLine = new Line();
DistanceLine.Stroke = new SolidColorBrush(LineColor);
DistanceLine.StrokeDashArray = new DoubleCollection() {0, 4};
DistanceLine.StrokeDashCap = PenLineCap.Round;
DistanceLine.StrokeEndLineCap = PenLineCap.Round;
DistanceLine.StrokeLineJoin = PenLineJoin.Round;
DistanceLine.StrokeStartLineCap = PenLineCap.Round;
DistanceLine.StrokeThickness = 3;
我想要这样的东西 = x----------------x dashed line with 'x' marks
如何在线条的末尾制作自定义形状?
评论建议并鼓励我写下自己的形状放在线帽处。这可能不是最好的方法,但对我来说效果很好。 class returns 一个作为网格的对象,当我画一条线时,我可以将其添加到我的 canvas 中。我是这样做的:-
public class CrossHair : Grid
{
public string LineName { get; set; }
/// <summary>
/// Draws the crosshair at the given point
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
static public CrossHair DrawCrossHair(double x, double y)
{
var crosshair = new CrossHair(); // to contain the cross hair
var line1 = new Line();
var line2 = new Line();
var line3 = new Line();
var line4 = new Line();
line1.Stroke = line2.Stroke = line3.Stroke = line4.Stroke = new SolidColorBrush(Colors.Yellow);
line1.StrokeThickness = line2.StrokeThickness = line3.StrokeThickness = line4.StrokeThickness = 2;
line1.X1 = x - 5;
line1.Y1 = y;
line1.X2 = x - 2;
line1.Y2 = y;
line2.X1 = x;
line2.Y1 = y + 5;
line2.X2 = x;
line2.Y2 = y + 2;
line3.X1 = x + 2;
line3.Y1 = y;
line3.X2 = x + 5;
line3.Y2 = y;
line4.X1 = x;
line4.Y1 = y - 2;
line4.X2 = x;
line4.Y2 = y - 5;
crosshair.Children.Add(line1);
crosshair.Children.Add(line2);
crosshair.Children.Add(line3);
crosshair.Children.Add(line4);
return crosshair;
}
}