The Border control in WPF provides a way to organize an interface
into parts. We use Border to draw colored, visual borders within a Window.
First create a WPF application. In the Toolbox, select Border and drag it to your window in the designer. We can then change attributes of the Border.
This example shows some features of a Border. Here we change the Border attributes within the XAML file—here are some of the more useful properties.
BorderBrush
to a named color. In this example, I use a red color: Tomato.BorderThickness
attribute indicates the number of pixels wide the border itself is.HorizontalAlignment
and VerticalAlignment
attributes to Stretch. These expand (anchor) the border to the container.Margin
value in the border to equal 10 on each side. We could use the value "10" as the entire margin value.<Window x:Class="WpfApplication5.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <Border BorderBrush="Tomato" BorderThickness="3" HorizontalAlignment="Stretch" Margin="10,10,10,10" VerticalAlignment="Stretch"/> </Grid> </Window>
Another element can be nested within a Border element. This means it is possible to use Border to create a section of user interface with sub-controls.
<Window x:Class="WpfApplication5.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <Border BorderBrush="Tomato" BorderThickness="3" HorizontalAlignment="Stretch" Margin="10" VerticalAlignment="Stretch"> <Border BorderBrush="Aqua" BorderThickness="3" Margin="10"/> </Border> </Grid> </Window>
We can add colors to Borders, adjust their thickness, and change their alignments—even stretch them. We can nest other controls within a Border.