Files
Andy De George da363692ff Initial WPF content migrated (#17)
* Reset branch for WPF changes

* Convert BMP to PNG; fix link-out-of-scope err

* Add snippets for WPF... 6794 files!!!!

* Add missing snippets

* update file updated between migration

* Fix paths to include

* update breadcrumb and toc

* fix index links

* fix index links

* fix index links

* fix markdown
2020-09-04 09:46:28 -07:00

2.7 KiB

title, ms.date, helpviewer_keywords, ms.assetid
title ms.date helpviewer_keywords ms.assetid
How to: Draw a Rectangle 03/30/2017
drawing [WPF], rectangles
graphics [WPF], rectangles
rectangles [WPF], drawing
beeb57ef-fab5-4446-a38a-1588f97b4c2f

How to: Draw a Rectangle

This example shows how to draw a rectangle by using the xref:System.Windows.Shapes.Rectangle element.

To draw a rectangle, create a xref:System.Windows.Shapes.Rectangle element and specify its xref:System.Windows.FrameworkElement.Width%2A and xref:System.Windows.FrameworkElement.Height%2A. To paint the inside of the rectangle, set its xref:System.Windows.Shapes.Shape.Fill%2A. To give the rectangle an outline, use its xref:System.Windows.Shapes.Shape.Stroke%2A and xref:System.Windows.Shapes.Shape.StrokeThickness%2A properties.

To give the rectangle rounded corners, specify the optional xref:System.Windows.Shapes.Rectangle.RadiusX%2A and xref:System.Windows.Shapes.Rectangle.RadiusY%2A properties. The xref:System.Windows.Shapes.Rectangle.RadiusX%2A and xref:System.Windows.Shapes.Rectangle.RadiusY%2A properties set the x-axis and y-axis radii of the ellipse that is used to round the corners of the rectangle.

In the following example, two xref:System.Windows.Shapes.Rectangle elements are drawn in a xref:System.Windows.Controls.Canvas. The first rectangle has a xref:System.Windows.Media.Brushes.Blue%2A interior. The second rectangle has a xref:System.Windows.Media.Brushes.Blue%2A interior, a xref:System.Windows.Media.Brushes.Black%2A outline, and rounded corners.

Example

[!code-xamldrawingwithshapeelements#Rectangle1]

Although this example uses a xref:System.Windows.Controls.Canvas to contain the rectangles, you can use rectangle elements (and all the other shape elements) with any xref:System.Windows.Controls.Panel or xref:System.Windows.Controls.Control that supports non-text content. In fact, rectangles are particularly useful for providing backgrounds for portions of xref:System.Windows.Controls.Grid panels. For an example, see the Table Overview.

This example is part of a larger sample; for the complete sample, see Shape Elements Sample.

See also