Wrapping UI elements using a Border
The Border
control in WPF is used as a Decorator
, which you can use to draw a border around another control. As the WPF panels do not support adding a border around its edges, the Border
control is used to achieve the same.
This recipe will guide you to add a border to a control. You can also use the same concept to decorate a group of controls placed inside a panel, by wrapping the panel with a Border
.
Getting ready
To begin with an example, let's first create a new project. Open Visual Studio and create a WPF application project named CH03.BorderDemo
.
How to do it...
Perform the following simple steps to add a border around TextBlock
:
- Open the
MainWindow.xaml
file of your WPF project. - Now replace the default
Grid
with aStackPanel
. - Add a few TextBlocks inside it, wrapped by a Border. Here's the complete XAML code:
<StackPanel Margin="10"> <Border BorderBrush="OrangeRed" BorderThickness="2" Margin="10 4" Padding="10">...