Shapes and Drawing
In Silverlight, a Shape is a type of UIElement that enables you to draw a shape to the screen. Because they are user interface (UI) elements, Shape objects can be used inside a variety of container objects such as Grid and Canvas.
This topic contains the following sections.
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.
The Line class enables you to draw a line between two points. The following example shows several ways to specify line coordinates and stroke properties.
<Canvas Height="300" Width="300"> <!-- Draws a diagonal line from (10,10) to (50,50). --> <Line X1="10" Y1="10" X2="50" Y2="50" Stroke="Black" StrokeThickness="4" /> <!-- Draws a diagonal line from (10,10) to (50,50) and moves it 100 pixels to the right. --> <Line X1="10" Y1="10" X2="50" Y2="50" StrokeThickness="10" Canvas.Left="100"> <Line.Stroke> <RadialGradientBrush GradientOrigin="0.5,0.5" Center="0.5,0.5" RadiusX="0.5" RadiusY="0.5"> <RadialGradientBrush.GradientStops> <GradientStop Color="Red" Offset="0" /> <GradientStop Color="Blue" Offset="0.5" /> </RadialGradientBrush.GradientStops> </RadialGradientBrush> </Line.Stroke> </Line> <!-- Draws a horizontal line from (10,60) to (150,60). --> <Line X1="10" Y1="60" X2="150" Y2="60" Stroke="Black" StrokeThickness="4"/> </Canvas>
Another common shape is the Ellipse. Create an Ellipse by defining the shape's Width and Height properties, as shown in the following example. To draw a circle, specify an Ellipse whose Width and Height values are equal.
The Path class enables you to draw curves and complex shapes. These curves and shapes are described by using Geometry objects. To use a Path, you create a Geometry and use it to set the Path object's Data property. There are a variety of Geometry objects to choose from. The LineGeometry, RectangleGeometry, and EllipseGeometry classes describe relatively simple shapes. To create more complex shapes or create curves, use a PathGeometry. For information about creating PathGeometry objects, see Geometries.
XAML Abbreviated Syntax
In XAML, you can use a special abbreviated syntax to describe a Path. In the following example, abbreviated syntax is used to draw a complex shape.
The following illustration shows the rendered Path.
The Data attribute string uses a mini-language. It begins with the move command, indicated by M, which establishes a start point for the path in the coordinate system of the Canvas. Path data parameters are case-sensitive. The capital M indicates an absolute location for the new current point. A lowercase m would indicate relative coordinates. The first segment is a cubic Bezier curve that begins at (100,200) and ends at (400,175), which is drawn by using the two control points (100,25) and (400,350). This segment is indicated by the C command in the Data attribute string. Again, the capital C indicates an absolute path; the lowercase c would indicate a relative path.
The second segment begins with an absolute horizontal line command H, which specifies a line drawn from the preceding subpath's endpoint (400,175) to a new endpoint (280,175). Because it is a horizontal line command, the value specified is an x-coordinate.
You use Brush objects to paint a shape's Stroke and Fill. In the following example, the stroke and fill of an Ellipse are specified. Note that valid input for brush properties can be either keywords or hexadecimal color values.
The following illustration shows the rendered Ellipse.
Alternatively, you can use property element syntax to explicitly create a SolidColorBrush object to paint the shape with a solid color.
<Canvas> <!-- This polygon shape uses predefined color values for its Stroke and Fill properties. The SolidColorBrush Opacity property affects the fill color in this case by making it slightly transparent (opacity of 0.4) so that it blends with any underlying color. --> <Polygon Points="300,200 400,125 400,275 300,200" Stroke="Purple" StrokeThickness="2"> <Polygon.Fill> <SolidColorBrush Color="Blue" Opacity="0.4"/> </Polygon.Fill> </Polygon> </Canvas>
The following illustration shows the rendered polygon.
You can also paint a shape's stroke or fill with gradients, images, patterns, and more. For more information, see Brushes.
Transform objects can be used to transform shapes (or any other UIElement) in a two-dimensional plane. The different types of transformations include rotation (RotateTransform), scale (ScaleTransform), skew (SkewTransform), and translation (TranslateTransform).
A common transform to apply to a shape is a rotation. To rotate a shape, create a RotateTransform and specify its Angle. An Angle of 45 rotates the element 45 degrees clockwise, an angle of 90 rotates the element 90 degrees clockwise, and so on. Set the CenterX and CenterY properties if you want to control the point about which the element is rotated. These property values are expressed in the coordinate space of the element that is being transformed. CenterX and CenterY have default values of 0. Finally, apply the RotateTransform to the element by setting the shape's RenderTransform property.
The following illustration shows how this code is rendered.
To apply multiple transforms to a shape (or to any other UI element), use a TransformGroup.
For more information about Transform objects and additional examples, see Transforms.