Drawing in WP7: #1 Getting Started and Line Shape
published on: 3/8/2011 | Views: N/A | Tags: Drawing UI
by WindowsPhoneGeek
I am starting a series of article in which I am going to talk about Drawings in Windows Phone 7.
- Drawing in WP7: #1 Getting Started and Line Shape
- Drawing in WP7: #2 Drawing shapes with finger
- Drawing in WP7: #3 Understanding Path shapes
Generally in Silverlight for Windows Phone 7 a Shape is a type of UIElement that enables you to draw a shape to the screen.
Understanding Shapes
According to the official documentation Silverlight provides a number of ready-to-use Shape objects, including Ellipse,Line, Path, Polygon, Polyline, and Rectangle. Shape objects share the following common properties:
-
Stroke : Describes how the shape's outline is painted.
-
StrokeThickness : Describes the thickness of the shape's outline.
-
Fill : Describes how the interior of the shape is painted.
-
Data properties to specify coordinates and vertices, measured in device-independent pixels.
Usually you use Shape objects inside a variety of container objects such as Grid and Canvas. Canvas supports absolute positioning of its child objects through the use of the Canvas.Left and Canvas.Top attached properties.
For reference visit the official MSDN documentation.
Intro to Line Drawing
One of the most important Shape when talking about drawings is the Line class because it enables you to draw a line between two points.
- X1is the x-coordinate of the Line start point
- X2 is the x-coordinate of the Line end point
- Y1 is the y-coordinate of the Line start point
- Y2 is the y-coordinate of the Line end point.
You can set the StrokeStartLineCap to one of the PenLineCap enum values (describes the shape at the end of a line or segment: Flat, Square, Round or Triangle).
You can draw a line either in XAML or code behind:
XAML:
<Canvas x:Name="ContentPanelCanvas" Grid.Row="1" Background="Transparent" Margin="12,0,12,0">
<Line X1="10" Y1="100" X2="150" Y2="100" Stroke="Green" StrokeThickness="5"/>
</Canvas>
C#:
NOTE: When add a point programmatically you have to set the background of the Canvas either to Transparent or to any particular color. Otherwise the line will not be visible.
Line line = new Line();
line.Stroke = new SolidColorBrush(Colors.Purple);
line.StrokeThickness = 15;
Point point1 = new Point();
point1.X = 10.0;
point1.Y = 100.0;
Point point2 = new Point();
point2.X = 150.0;
point2.Y = 100.0;
line.X1 = point1.X;
line.Y1 = point1.Y;
line.X2 = point2.X;
line.Y2 = point2.Y;
this.ContentPanelCanvas.Children.Add(line);

I hope that this post was helpful.
You can also follow us on Twitter @winphonegeek
Comments
thank you
posted by: Indrek on 4/16/2011 1:39:26 AM
Thank you, really nice tutorial. Saved me a lot of time!
posted by: sagar on 5/12/2011 1:43:15 PM
thanks for the valuable information.
Our Top Articles & Free books
- Our FREE e-book: "Windows Phone Toolkit In Depth" 2nd edition
- 400+ Windows Phone Development articles in our Article Index
- 21 WP7 Toolkit in Depth articles covering all controls
- 12 WP7 Coding4Fun Toolkit in Depth articles covering all controls
- Performance Tips when creating WP7 apps
- Creating a WP7 Custom Control in 7 Steps
- WP7 working with VisualStates: How to make a ToggleSwitch from CheckBox
- What makes a WP7 App successful
- Creating theme friendly UI in WP7 using OpacityMask
- Implementing Windows Phone 7 DataTemplateSelector and CustomDataTemplateSelector
- All about Splash Screens in WP7 – Creating animated Splash Screen
- Getting Started with Unit Testing in Silverlight for WP7
- WP7 WatermarkedTextBox custom control
Our Top Tips & Samples
- All about WP7 Isolated Storage series
- WP7 Dynamically Generating DataTemplate in code
- 5 tips for a successful WP7 Marketplace submission
- WP7: Navigating to a page in different assembly
- WP7 ContextMenu: answers to popular questions
- WP7 ListBox: answers to popular questions
- WP7 working with Images: Content vs Resource build action
- WP7 Element Binding samples
- WP7 working with XML: reading, filtering and databinding
- Drawing in WP7: #2 Drawing shapes with finger
- WP7 TextBox Light theme problems - the solution
- Changing the WP7 Panorama Background Image dynamically with Animation
