The Border control is a Decorator control that you may use to draw a border, a background, or even both, around another element. Since the WPF panels don't
support drawing a border around its edges, the Border control can help you achieve just that, simply by surrounding e.g. a Panel with the Border control.
A simple example on using the Border as described above could look like this:
The Border is completely lookless until you define either a background or a border brush and thickness, so that's what I've done here, using the
Background
,
BorderBrush
and
BorderThickness
properties.
Border with round corners
One of the features I really appreciate about the Border is the fact that it's so easy to get round corners. Just look at this slightly modified example,
where the corners are now rounded:
All I've done is adding the
CornerRadius
property. It can be specified with a single value, which will be used for all four corners, or
like I did in the example here, where I specify separate values for the top right and left followed by the bottom right and left.
Border color/thickness
The above border is very discrete, but this can easily be changed by regulating the color and/or thickness. Because the BorderThickness property is of the
Thickness
type, you can even manipulate each of the border widths individually or by giving a value for the left and right and one for the
top and bottom borders.
The Background property is of the type Brush, which opens up a lot of cool possibilities. As seen in the initial examples, it's very easy to just use a
simple color as the background, but you can actually use gradients as well, and it's not even that hard to do:
In this case, I've specified a
LinearGradientBrush
to be used for the background of the Border and then a more fitting border color. The
LinearGradientBrush might not have the most obvious syntax, so I will explain that in a later chapter, including other brush types, but for now, you can
try my example and change the values to see the result.