Home
Map
Rectangle: Fill and StrokeUse the Rectangle control to visually specify a rectangular region with a color and border.
WPF
This page was last reviewed on Sep 28, 2022.
Rectangle. Shapes can be part of WPF interfaces. With the Rectangle, we specify a rectangular graphical region that can have different colors and borders.
Getting started. First, please drag a Rectangle from the Visual Studio Toolbox to your WPF Window. Next, you can adjust its attributes.
Attributes example. By dragging some of the guides, you can add a RadiusY and RadiusX: these give the rectangle rounded corners. Next, try adding the Stroke attribute in the XAML.
Tip You can adjust the thickness of the Stroke (border) by adding a StrokeThickness attribute, which counts in pixels.
Tip 2 You can apply a colored background to the rectangle with the Fill attribute. This can receive hex colors or the named colors.
Finally You will need to adjust Width and Height as well. This will determine the size of the rectangle.
<Window x:Class="WpfApplication16.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <Rectangle Fill="Beige" HorizontalAlignment="Left" Height="100" Margin="10,10,0,0" Stroke="DarkMagenta" StrokeThickness="5" VerticalAlignment="Top" Width="150" RadiusY="13.5" RadiusX="13.5"/> </Grid> </Window>
Ellipse. With the RadiusX and RadiusY attributes, you can create an Ellipse with a rectangle. But using an Ellipse control directly may be simpler.
Tip In the Ellipse example, I show how mouse events can interact with shapes. This is equally true with rectangles.
Ellipse
Summary. With WPF, a main goal is to make programs easier and faster to create. The Rectangle control help this goal: they are easy options for when a graphical element (shape) is needed.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Sep 28, 2022 (edit).
Home
Changes
© 2007-2024 Sam Allen.