WPF Graphics: Drawing a square using XAML

This entry is part 6 of 8 in the series WPF Graphics

Drawing a square using XAML.

The canvas.left and canvas.right position the location of the rectangle relative to the position of the canvas.

MainWindow.xaml

<Window x:Class="WpfApplication11.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication11"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Canvas Width="300" Height="300">
            <Rectangle Width="250" Height="250" Canvas.Left="20" Canvas.Top="20" Fill="LightBlue"/>
        </Canvas>
    </Grid>

MainWindow.xaml.cs

using System.Windows;

namespace WpfApplication11
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
}

square

Series Navigation<< WPF Graphics: Drawing a circle with code
WPF Graphics: Drawing a square using code behind >>