Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

How to Create a Simple Arrow in Silverlight?

0.00/5 (No votes)
26 Jan 2010 1  
In this post, I will discuss how to create a Silverlight arrow.

In this post, I will discuss how to create a Silverlight arrow. This will be beneficial for implementing Silverlight presentation where you can add some arrows to point to some blocks. This is simple enough to implement & will be beneficial for beginners.

Create a Silverlight project. Now add a public class “Arrow” in it. Inside the Arrow.cs, you have to add three lines (one is the base line, the second is the left arrow head & the third is the right arrow head). Next, you have to add those three lines inside a Panel.

Let’s start with creating the simple arrow in a step-by-step process. The first step is to create the base line of the arrow. Create a Line and set its (x1, y1) & (x2, y2). This will create the line.

private static Line CreateBaseLine
    (double startX, double startY, double length, Brush lineBrush, double thickness)
        {
            Line arrowLine = new Line();
            arrowLine.Stroke = lineBrush;
            arrowLine.StrokeThickness = thickness;
            arrowLine.X1 = startX;
            arrowLine.X2 = length;
            arrowLine.Y1 = startY;
            arrowLine.Y2 = startY;
            return arrowLine;
        }

Now let's create the left arrow head. The approach to it is the same as creating the base line. Here only, you have to pass the instance of the base line for calculating the (x1, y1).

private static Line CreateLeftArrowHead(Brush lineBrush, double thickness, Line arrowLine)
        {
            Line arrowLeft = new Line();
            arrowLeft.Stroke = lineBrush;
            arrowLeft.StrokeThickness = thickness;
            arrowLeft.X1 = arrowLine.X2 - 10.00;
            arrowLeft.X2 = arrowLine.X2;
            arrowLeft.Y1 = arrowLine.Y2 - 10.00;
            arrowLeft.Y2 = arrowLine.Y2;
            return arrowLeft;
        }

Creation of right arrow head is also similar to the one I mentioned for creating the left arrow head. The difference here is the y1.

private static Line CreateRightArrowHead
	(Brush lineBrush, double thickness, Line arrowLine)
        {
            Line arrowRight = new Line();
            arrowRight.Stroke = lineBrush;
            arrowRight.StrokeThickness = thickness;
            arrowRight.X1 = arrowLine.X2 - 10.00;
            arrowRight.X2 = arrowLine.X2;
            arrowRight.Y1 = arrowLine.Y2 + 10.00;
            arrowRight.Y2 = arrowLine.Y2;
            return arrowRight;
        }

Here, instead of creating two separate methods for creating the two arrow heads, you can just create only one method & pass the calculated (x1, y1) & (x2, y2). This is the simple approach mentioned here to give an idea to the beginner to create an arrow. It can be modified to implement some complex arrows like Horizontal, Vertical or even better Connected Arrows.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here