Reputation: 300
I'm using System.Windows.Shapes.Line in my application to draw a line on a canvas. I want to put a shape e.g. a cross ('x') at the start and end of the line? Is there any way to do this by setting a property. I can add a 'x' on the canvas based on the coordinates but I was hoping if we can do this using some Line property directly. Currently I'm able to draw this = --------------- dashed line using the properties as in the snippet under:-
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;
I want something like this = x------------------x dashed line with 'x' marks
How can I make a custom shape at the end of the lines?
Upvotes: 2
Views: 1839
Reputation: 300
The comments suggested and encouraged me to write my own shape to put at the line caps. This may not be the best approach but works for me well. The class returns an object as a Grid which I can add to my canvas when I draw a line. This is how I've done it:-
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;
}
}
Upvotes: 2