Chapter 9. Shapes and Geometry


Shape and Geometry are both used to render 2-D objects. While the two have much in common, there are important differences between the two sets of drawing objects. The most important is that instances of Geometry cannot draw themselves; they must be drawn by another class. There are other differences, but the easiest way to differentiate the two is to remember that Geometry is used to describe a region and Shape determines how that region is drawn and filled. Because Shapes are UI elements, they can be used inside panels and most controls. Geometry elements cannot.

Geometry elements are also used to define clipping regions . A clipping region defines the visible area of another element, such as an Image. For example, if you have a large image but only want to display part of it, you could use a Geometry element to clip it. You could also use Geometry elements to clip the image to simulate a frame, as Example 9-1 and Figure 9-1 illustrate.

Example 9-1. Clipping an image with EllipseGeometry

 <StackPanel     xmlns="http://schemas.microsoft.com/winfx/avalon/2005"     Margin="20">     <Image         Source="c:\image.jpg"         HorizontalAlignment="Left">         <Image.Clip>             <EllipseGeometry                 RadiusX="100"                 RadiusY="75"                 Center="100,75"/>         </Image.Clip>     </Image> </StackPanel> 

Figure 9-1. Using EllipseGeometry to clip an image


Geometries are categorized as either simple or path. A simple geometry is used to describe basic geometric shapes, such as line, ellipse, and rectangle. Path geometry describes more complex geometric figures, such as ones created by tracing a path.

Although shapes are most commonly drawn on a Canvas, they may be used with any Panel or control that supports non-text elements. This chapter details the shapes and geometry available for use in XAML.

All shapes derive from the base class Shape and therefore share a common set of attributes, which is detailed in Table 9-1.

Table 9-1. Attributes common to all Shape elements

Property name

Data type

Purpose

Fill

Brush

Describes how the shape's interior is filled. The default is null. A list of pre-defined Brush colors is in Appendix G.

StrokeDashArray

DoubleCollection

Describes the series of dashes and gaps used to outline the shape. Each Double in the collection specifies the length of a dash or gap relative to the thickness of the pen. For example, a value of 1 creates a dash or gap with the same length as the thickness of the pen (a square). The first item in the collection, located at index 0, specifies the length of a dash; the second item, located at index 1, specifies the length of a gap; and so on. Objects with an even index value specify dashes, and objects with an odd index value specify gaps.

StrokeDashCap

Enumeration

Describes how the ends of a dash are drawn. Must be one of the following:


Flat

No line cap.


Round

The line is capped with a semicircle equal in diameter to the line thickness.


Square

The line is capped with a square whose sides are equal in length to the line thickness.


Triangle

The line is capped with a triangle equal in height to the line thickness.

The default is Flat.

StrokeDashOffset

Double

Describes the distance in the dash pattern at which the dash will start.

StrokeEndLineCap

Enumeration

Describes the shape used at the end of the element's stroke. Must be one of the following:


Flat

No line cap.


Round

The line is capped with a semicircle equal in diameter to the line thickness.


Square

The line is capped with a square whose sides are equal in length to the line thickness.


Triangle

The line is capped with a triangle equal in height to the line thickness.

The default is Flat.

StrokeLineJoin

Enumeration

Sets the type of joint used at the vertices of a shape's outline. Must be one of the following:


Bevel

Beveled vertices


Miter

Regular angular vertices


Round

Rounded vertices

StrokeMiterLimit

Double

Specifies a limit on the ratio of the miter length to the StrokeThickness of a Shape element. The value is always greater than or equal to 1.

Stroke

Brush

Describes how the outline of the shape will be drawn. The default is null.

StrokeStartLineCap

Enumeration

Describes the shape used to draw the start of a line. Must be one of:


Flat

No line cap.


Round

The line is capped with a semicircle equal in diameter to the line thickness.


Square

The line is capped with a square whose sides are equal in length to the line thickness.


Triangle

The line is capped with a triangle equal in height to the line thickness.

The default is Flat.

StrokeThickness

Double

Sets the width of the shape's outline.

Height

Double

Describes the height of the element.

Width

Double

Describes the width of the element.


Most Shape and Geometry elements use instances of the structure Point, which is detailed in this chapter.




XAML in a Nutshell
XAML in a Nutshell (In a Nutshell (OReilly))
ISBN: 0596526733
EAN: 2147483647
Year: 2007
Pages: 217

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net