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" />
    </StackPanel>
  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 books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.