SIGN UP MEMBER LOGIN:    
ARTICLE

Line in WPF

Posted by Mahesh Chand Articles | WPF February 02, 2010
Tags: Line, Line class, WPF, XAML
The Line object represents a line shape and draws a line between two defined points. This article demonstrates how to create lines in WPF and XAML.
Reader Level:

The Line object represents a line shape and draws a line between two defined points. The X1 and Y1 properties of the Line represent the start point and X2 and Y2 properties represent the end point of the line. The Stroke property sets the color of the line and StrokeThickness represents the width of the line. A line shape does not have an interior so Fill property has no affect on a line.

Creating a Line

The Line element in XAML creates a line shape. The following code snippet creates a Line by setting its start point (X1, Y1) to (50, 50) and end point (X2, Y2) to (200, 200). That means a line is drawn from point (50, 50) to (200, 200). The code also sets the color of the line to red and width 4.

<Line

    X1="50" Y1="50"

    X2="200" Y2="200"

    Stroke="Red"

    StrokeThickness="4" />

The output looks like Figure 4.

LineImg.gif

Figure 4. A line

The CreateALine method listed in Listing 6 draws same line in Figure 4 dynamically. LayoutRoot in the below code is ID of a Grid panel on the page.

/// <summary>

/// Creates a line at run-time

/// </summary>

public void CreateALine()

{

    // Create a Line

    Line redLine = new Line();

    redLine.X1 = 50;

    redLine.Y1 = 50;

    redLine.X2 = 200;

    redLine.Y2 = 200;

 

    // Create a red Brush

    SolidColorBrush redBrush = new SolidColorBrush();

    redBrush.Color = Colors.Red;

 

    // Set Line's width and color

    redLine.StrokeThickness = 4;

    redLine.Stroke = redBrush;

 

    // Add line to the Grid.

    LayoutRoot.Children.Add(redLine);

}

Listing 6




share this article :
post comment
 

What if you want to map the line's coordinates to points in a grid rather than to fixed number values?

Posted by Daniel Jun 17, 2010

Try this Stroke="#afefad"

Posted by Mahesh Chand May 29, 2010

hi 

what if i want to set a custom color to a line like #aeaeae

Posted by saboor ahmed May 29, 2010
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor