Proper layout and positioning are a vital part of interactive, high-performance and user-friendly Windows applications. This series of articles explains the layout process in WPF. The series starts with an understanding of the WPF layout process. The next part of this series will cover the basics of layout and positioning such as size, margin, padding and alignment of elements. Later in this series, I will cover various panels and related parent controls available in WPF.
Table of Contents
- WPF Layout System: An Introduction
- WPF Layout: Size, Width and Height
- WPF Layout: Margins
- WPF Layout: Horizontal and Vertical Alignment
- WPF Layout: Content Alignments
- WPF Layout: Dealing with Percentage Size in WPF
- WPF Layout: Panels
- WPF Layout: Canvas
- WPF Layout: DockPanel
- WPF Layout: Grid
- WPF Layout: StackPanel
- WPF Layout: WrapPanel
- WPF Layout: Border
- WPF Layout: VirtualizingStackPanel
Introduction
In the previous article, WPF Layout: Panels, I discussed the basics of panels. This article focuses on the Canvas Panel in details.
Canvas Panel
A Canvas panel is used to position child elements using coordinates relative to the canvas area. Here are some of the properties of Canvas panels.
- The default values of the Height and Width properties of a Canvas are 0. If you do not set these values, you will not see a canvas unless child elements are automatically resizable.
- Child elements on a Canvas are never resized.
- The vertical and horizontal alignments on child elements do not work. Child elements are placed on positions set by the Canvas Left, Top, Right and Bottom properties.
- Margin does work partially. If a Left property of a Canvas is set, Right property does not work. If a Top property of a Canvas is set, the Bottom property does not work.
The Canvas element in XAML represents a Canvas control.
- <Canvas/>
The code in Listing 1 creates a Canvas and adds three Rectangle controls and positions them using Canvas control properties.
- <Canvas Background="LightCyan" >
- <Rectangle
- Canvas.Left="10" Canvas.Top="10"
- Height="200" Width="200"
- Stroke="Black" StrokeThickness="10" Fill="Red" />
- <Rectangle
- Canvas.Left="60" Canvas.Top="60"
- Height="200" Width="200"
- Stroke="Black" StrokeThickness="10" Fill="Blue" />
- <Rectangle
- Canvas.Left="110" Canvas.Top="110"
- Height="200" Width="200"
- Stroke="Black" StrokeThickness="10" Fill="Green" />
- </Canvas>
The output looks as in Figure 1.

Figure 1
The Canvas class in WPF represents a Canvas control. The code listed in Listing 2 creates a Canvas Panel dynamically, adds three Rectangle controls to it and sets their left and top positions using Canvas.SetLeft and Canvas.SetTop methods. The output of Listing 1 generates Figure 1.
- private void CreateDynamicCanvasPanel()
- {
- // Create a Canvas Panel control
- Canvas canvasPanel = new Canvas();
- // Set Canvas Panel properties
- canvasPanel.Background = new SolidColorBrush(Colors.LightCyan );
- // Add Child Elements to Canvas
- Rectangle redRectangle = new Rectangle();
- redRectangle.Width = 200;
- redRectangle.Height = 200;
- redRectangle.Stroke = new SolidColorBrush(Colors.Black);
- redRectangle.StrokeThickness = 10;
- redRectangle.Fill = new SolidColorBrush(Colors.Red);
- // Set Canvas position
- Canvas.SetLeft(redRectangle, 10);
- Canvas.SetTop(redRectangle, 10);
- // Add Rectangle to Canvas
- canvasPanel.Children.Add(redRectangle);
- // Add Child Elements to Canvas
- Rectangle blueRectangle = new Rectangle();
- blueRectangle.Width = 200;
- blueRectangle.Height = 200;
- blueRectangle.Stroke = new SolidColorBrush(Colors.Black);
- blueRectangle.StrokeThickness = 10;
- blueRectangle.Fill = new SolidColorBrush(Colors.Blue);
- // Set Canvas position
- Canvas.SetLeft(blueRectangle, 60);
- Canvas.SetTop(blueRectangle, 60);
- // Add Rectangle to Canvas
- canvasPanel.Children.Add(blueRectangle);
- // Add Child Elements to Canvas
- Rectangle greenRectangle = new Rectangle();
- greenRectangle.Width = 200;
- greenRectangle.Height = 200;
- greenRectangle.Stroke = new SolidColorBrush(Colors.Black);
- greenRectangle.StrokeThickness = 10;
- greenRectangle.Fill = new SolidColorBrush(Colors.Green);
- // Set Canvas position
- Canvas.SetLeft(greenRectangle, 110);
- Canvas.SetTop(greenRectangle, 110);
- // Add Rectangle to Canvas
- canvasPanel.Children.Add(greenRectangle);
- // Set Grid Panel as content of the Window
- RootWindow.Content = canvasPanel;
- }
The z-order of a control determines whether the control is in front of or behind another overlapping control. The default z-order of controls is the order controls are created in. The ZIndex property of Canvas represents the z-order of a control. The maximum value of ZIndex is 32766.
In Figure 1, the z-order of the Red, Blue and Green rectangles is 1, 2 and 3 respectively. Now, the code in Listing 3 changes the z-order of these rectangles using the ZIndex property of the Canvas.
The code snippet in Listing 3 sets the position of the Canvas control in the top-left corner of the page.
- <Canvas Background="LightCyan" >
- <Rectangle
- Canvas.Left="10" Canvas.Top="10"
- Height="200" Width="200"
- Stroke="Black" StrokeThickness="10"
- Fill="Red" Canvas.ZIndex="2" />
- <Rectangle
- Canvas.Left="60" Canvas.Top="60"
- Height="200" Width="200"
- Stroke="Black" StrokeThickness="10"
- Fill="Blue" Canvas.ZIndex="1" />
- <Rectangle
- Canvas.Left="110" Canvas.Top="110"
- Height="200" Width="200"
- Stroke="Black" StrokeThickness="10"
- Fill="Green" Canvas.ZIndex="3" />
- </Canvas>
The new output looks as in Figure 2 where Blue rectangle is below the Red and the Green is on top of both of them.

Figure 2
A Dock Panel is used to dock child elements in the left, right, top and bottom positions of the relative window.
Summary
In this article, I discussed the Canvas panel in WPF. In the next article of this series, I will focus on Dock panel in details.

AnilPosted Jan 2, 2019, 5:38 AM
Thanks for your Good Articles. I have gone through previous articles of tpoic WPF Layout, in every part you have done Dynamically creating UI . I need to know what does this dynamically means , Is it creating UI instead of XAML page ? If creating UI from c# then how that function gets called, for example in this article function "CreateDynamicCanvasPanel() " is used. I need to know how and where does that this functions gets called
ROHAN PANDEYPosted Mar 19, 2015, 11:27 AM
Thanks sir