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:

  1. Create a new WPF application named CH03.BorderDemo.
  2. Open MainWindow.xaml. Replace the existing Grid with a StackPanel, and add a TextBlock inside it, like the following markup shows:
    <StackPanel Margin="4">
        <TextBlock Text="Hello from Center" FontSize="25"
                   HorizontalAlignment="Center" />
  3. Running the application shows the text centered horizontally within the StackPanel ...

Get Windows Presentation Foundation 4.5 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.