Creating a border around panels and elements
Sometimes a simple decoration is required around some element or panel, such as a border. Luckily, the Border
element does just that to anything placed inside it.
Getting ready
Make sure Visual Studio is up and running.
How to do it...
We'll create a simple border encompassing some text, to show most of the capabilities of Border
:
Create a new WPF application named
CH03.BorderDemo
.Open
MainWindow.xaml
. Replace the existingGrid
with aStackPanel
, and add aTextBlock
inside it, like the following markup shows:<StackPanel Margin="4"> <TextBlock Text="Hello from Center" FontSize="25" HorizontalAlignment="Center" /> </StackPanel>
Running the application shows the text centered horizontally within the
StackPanel
.Suppose we want to draw a border around the
TextBlock
. Add aBorder
element around theTextBlock
as shown in the following markup:<Border BorderThickness="3" BorderBrush="Blue" CornerRadius="6">...