Home
WPF
Rectangle: Fill and Stroke
Updated Sep 28, 2022
Dot Net Perls
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 pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Sep 28, 2022 (edit).
Home
Changes
© 2007-2025 Sam Allen