Home
WPF
Border Example
This page was last reviewed on Apr 5, 2023.
Dot Net Perls
Border. 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.
Getting started. 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.
Example. 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.
Tip We can set the BorderBrush to a named color. In this example, I use a red color: Tomato.
Next The BorderThickness attribute indicates the number of pixels wide the border itself is.
Further We set the HorizontalAlignment and VerticalAlignment attributes to Stretch. These expand (anchor) the border to the container.
HorizontalAlignment
Here I changed the 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>
Example 2. 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.
And By changing the positioning of these controls, we simplify our program layout.
Here We nested an "aqua" border within our "tomato" border. We use the simplified margin syntax. We set the thickness to 3.
<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>
A summary. We can add colors to Borders, adjust their thickness, and change their alignments—even stretch them. We can nest other controls within a Border.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Apr 5, 2023 (edit).
Home
Changes
© 2007-2024 Sam Allen.