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
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.
The Canvas control has three properties. The Left property represents the distance between the left side of a control and its parent container Canvas. The Top property represents the distance between the top of a control and its parent container 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>
Listing 1
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()
- {
-
-
- Canvas canvasPanel = new Canvas();
-
-
- canvasPanel.Background = new SolidColorBrush(Colors.LightCyan );
-
- 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);
-
-
- Canvas.SetLeft(redRectangle, 10);
- Canvas.SetTop(redRectangle, 10);
-
-
-
- canvasPanel.Children.Add(redRectangle);
-
-
- 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);
-
-
- Canvas.SetLeft(blueRectangle, 60);
- Canvas.SetTop(blueRectangle, 60);
-
-
-
- canvasPanel.Children.Add(blueRectangle);
-
-
- 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);
-
-
- Canvas.SetLeft(greenRectangle, 110);
- Canvas.SetTop(greenRectangle, 110);
-
-
- canvasPanel.Children.Add(greenRectangle);
-
-
- RootWindow.Content = canvasPanel;
-
- }
Listing 2
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>
Listing 3
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.