How to put a border around a Canvas

This code snippet shows how to put a border around a Canvas using XAML Border element.

The BorderThickness property of the Border element sets the thickness of the border.

<Page x:Class="CanvasAbsolutePosition.Page1"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Title="Page1"

      Name="MainPage">

 

    <Border

        BorderThickness="2"

        BorderBrush="Black"

        Background="LightGray"

        HorizontalAlignment="Left"

        VerticalAlignment="Top"

        Width="402"

        Height="402">

 

        <Canvas Height="400" Width="400">

            <Rectangle Height="100" Width="100" Canvas.Top="0" Canvas.Left="0" Fill="Red"/>

            <Rectangle Height="100" Width="100" Canvas.Top="100" Canvas.Left="100" Fill="Green"/>

            <Rectangle Height="100" Width="100" Canvas.Top="50" Canvas.Left="50" Fill="Blue"/>

        </Canvas>

 

    </Border>

 

</Page>