Windows apps
Collapse the table of content
Expand the table of content
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Canvas Class

Defines an area within which you can explicitly position child elements by using coordinates that are relative to the Canvas area.

Namespace: System.Windows.Controls
Assembly: PresentationFramework (in presentationframework.dll)
XML Namespace:

public class Canvas : Panel
public class Canvas extends Panel
public class Canvas extends Panel

Content Model: Canvas enforces a strong content model for child content. See the Children property for more information about the Panel content model.

Canvas is the only panel element that has no inherent layout characteristics. A Canvas has default Height and Width properties of zero, unless it is the child of an element that automatically sizes its child elements. Child elements of a Canvas are never resized, they are just positioned at their designated coordinates. This provides flexibility for situations in which inherent sizing constraints or alignment are not needed or wanted. For cases in which you want child content to be automatically resized and aligned, it is usually best to use a Grid element.

The ZIndex property determines the order in which child elements that share the same coordinate space appear. A higher ZIndex value for one child element indicates that this element will appear above another child element that has a lower value.

If you specify them, the attached properties Top or Left take priority over the Bottom or Right properties.

Child elements of a Canvas are always given the full size that they desire. As a result, vertical alignment and horizontal alignment have no effect inside a Canvas.

Canvas is a top-level layout control that you can use for absolute positioning of child content. For painting and drawing, you use Brushes and do not have to use a Canvas. For more information, see WPF Brushes Overview.

By default, panel elements do not receive focus. To compel a panel element to receive focus, set the Focusable property to true.

This example shows how to create and use an instance of Canvas.

The following example explicitly positions two TextBlock elements by using the SetTop and SetLeft methods of Canvas. The example also assigns a Background color of LightSteelBlue to the Canvas.


When you use Extensible Application Markup Language (XAML) to position TextBlock elements, use the Top and Left properties.

    private void CreateAndShowMainWindow()
        // Create the application's main window
        mainWindow = new Window();
        // Create a canvas sized to fill the window
        Canvas myCanvas = new Canvas();
        myCanvas.Background = Brushes.LightSteelBlue;
        // Add a "Hello World!" text element to the Canvas
        TextBlock txt1 = new TextBlock();
        txt1.FontSize = 14;
        txt1.Text = "Hello World!";
        Canvas.SetTop(txt1, 100);
        Canvas.SetLeft(txt1, 10);
        // Add a second text element to show how absolute positioning works in a Canvas
        TextBlock txt2 = new TextBlock();
        txt2.FontSize = 22;
        txt2.Text = "Isn't absolute positioning handy?";
        Canvas.SetTop(txt2, 200);
        Canvas.SetLeft(txt2, 75);
        mainWindow.Content = myCanvas;
        mainWindow.Title = "Canvas Sample";

<Page WindowTitle="Canvas Sample" xmlns="">
  <Canvas Background="LightSteelBlue">
    <TextBlock FontSize="14" Canvas.Top="100" Canvas.Left="10">Hello World!</TextBlock>
    <TextBlock FontSize="22" Canvas.Top="200" Canvas.Left="75">Isn't absolute positioning handy?</TextBlock>

More Code

How to: Wrap a Border Around the Content of a Canvas

This example shows how to wrap a Canvas element with a Border.

How to: Absolutely Position Elements in a Canvas

This example shows how to use the Canvas control to absolutely position content.


Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0
© 2018 Microsoft