Shapes C# Help

Shapes are the core elements of WPF. With shapes you can draw 20 graphics using rectangles, lines, ellipses,paths, polygons, and polylines that are represented by classes derived from the abstract base class Shape. Shapes are defined in the namespace System Windows Shapes.

The following XAML example draws a yellow face with blue legs,consisting of an ellipsefor the face, two ellipses for the eyes, a path for the mouth, and four lines for the legs:

Figure 34-5 shows the result from the XAML code.

Figure 34-5

Figure 34-5

All of these WPF elements can be accessed programmatically, even if they are buttons or shapes such as lines or rectangles, Setting the Name property with the Path element to mouth allows you to access this element programmatically with the variable name mouth:

<Path Name=’mouth” Stroke=’Blue’ StrokeThickness=’4′
Data=’M 62,125 Q 95,122 102,108′ />

In the code-behind Data property of the Path element, mouth is set to a new geometry, For setting the path, the Path class supports PathGeometry with path markup syntax, The letter M defines the starting point for the path; the letter Q specifies a control point and an endpoint for a quadratic Bezier curve, Running the application, you-see the window shown in Figure 34-6.

public window                                                                                       InitializeComponent() ;
mouth.Data = Geometry.Parse(
‘M 62,125 Q 95,122 102,128’);

Figure 34-6

Figure 34-6

Earlier in this chapter, you learned that a button can have content, Making a small change to the XAML code and adding the Button element as content to the window causes the graphic to be displayed inside a button (see Figure 34-7).

Figure 34-7

Figure 34-7

Following are the shapes available in the namespace System. windows. Shapes.

Posted on November 3, 2015 in Windows Presentation Foundation

Share the Story

Back to Top