Click here to Skip to main content
15,886,840 members
Articles / Programming Languages / C#

How to Create a Simple Arrow in Silverlight?

Rate me:
Please Sign up or sign in to vote.
4.33/5 (5 votes)
26 Jan 2010CPOL1 min read 19.2K   5   2
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.

C#
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).

C#
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.

C#
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.

This article was originally posted at http://kunal2383.blogspot.com/feeds/posts/default

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
India India

Kunal Chowdhury is a former Microsoft "Windows Platform Development" MVP (Most Valuable Professional, 2010 - 2018), a Codeproject Mentor, Speaker in various Microsoft events, Author, passionate Blogger and a Senior Technical Lead by profession.

He is currently working in an MNC located in India. He has a very good skill over XAML, C#, Silverlight, Windows Phone, WPF and Windows app development. He posts his findings, articles, tutorials in his technical blog (www.kunal-chowdhury.com) and CodeProject.


Books authored:


Connect with Kunal on:





Comments and Discussions

 
GeneralMy vote of 2 Pin
Paul D'hertoghe1-Feb-10 23:39
professionalPaul D'hertoghe1-Feb-10 23:39 
GeneralRe: My vote of 2 Pin
Kunal Chowdhury «IN»15-Feb-10 19:19
professionalKunal Chowdhury «IN»15-Feb-10 19:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.