O'Reilly logo

Windows Presentation Foundation 4.5 Cookbook by Pavel Yosifovich

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required