Click to Rate and Give Feedback
MSDN
MSDN Library
Web Development
Silverlight 3
Canvas Class

  Switch on low bandwidth view
This page is specific to
Microsoft Silverlight 3

Other versions are also available for the following:
.NET Framework Class Library for Silverlight
Canvas Class

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

Namespace:  System.Windows.Controls
Assembly:  System.Windows (in System.Windows.dll)
Visual Basic (Declaration)
Public Class Canvas _
    Inherits Panel
Visual Basic (Usage)
Dim instance As Canvas
C#
public class Canvas : Panel
XAML Object Element Usage
<Canvas ...>
  oneOrMoreUIElements
</Canvas>

XAML Values

oneOrMoreUIElements

One or more object elements that derive from the UIElement class. Object elements defined here become members of the Children collection.

A Canvas is one of the Panel elements that enable layout. Each child object is rendered within the Canvas area. You control the positioning of objects inside the Canvas by specifying x and y coordinates. These coordinates are in pixels. The x and y coordinates are often specified by using the Canvas..::.Left and Canvas..::.Top attached properties. Canvas..::.Left specifies the object's distance from the left side of the containing Canvas (the x-coordinate), and Canvas..::.Top specifies the object's distance from the top of the containing Canvas (the y-coordinate).

NoteNote:

Because absolute positioning does not take into account the size of the browser window or browser resizing, using Grid or StackPanel as a container is often a better choice. For more information, see Silverlight Layout System.

You can nest Canvas objects. When you nest objects, the coordinates used by each object are relative to its immediate containing Canvas.

Each child object must be a UIElement. In XAML, you declare child objects as object elements that are the inner XML of a Canvas object element. In code, you can manipulate the collection of Canvas child objects by getting the collection that is accessed by the Children property.

You can nest Canvas objects, because a Canvas is a type of UIElement.

In many cases, a Canvas is used solely as a container for other objects and does not have any visible properties. A Canvas is not visible if any of the following conditions are true:

  • The Height property is equal to 0.

  • The Width property is equal to 0.

  • The Background property is equal to nullNothingnullptra null reference (Nothing in Visual Basic).

  • The Opacity property is equal to 0.

  • The Visibility property is equal to Visibility..::.Collapsed.

  • One of the ancestor objects of the Canvas is not visible.

The following example shows how to position a rectangle 30 pixels from the left and 30 pixels from the top of a Canvas.

<Canvas Width="640" Height="480" Background="White">
    <Rectangle Canvas.Left="30" Canvas.Top="30" 
               Fill="red" Width="200" Height="200" />
</Canvas>

The preceding example produces output that is similar to the following illustration.

Positioning a rectangle inside the Canvas

ms609101.JOLT_positioning_ovw1(en-us,VS.95).png

In the following example, the root (white) Canvas contains a nested (blue) Canvas that has Canvas..::.Left and Canvas..::.Top properties of 30. The nested blue Canvas contains a red rectangle that also has Canvas..::.Left and Canvas..::.Top values of 30. This creates the effect of a red rectangle bordered by a blue rectangle. This same layout could be achieved using a Border object.

<Canvas Width="300" Height="300" Background="White">
    <Canvas Width="250" Height="250" Canvas.Left="30" 
            Canvas.Top="30" Background="blue">

        <Rectangle Canvas.Left="30" Canvas.Top="30" 
                   Fill="red" Width="200" Height="200" />
    </Canvas>
</Canvas>

The preceding example produces output that is similar to the following illustration.

Nested objects

ms609101.JOLT_positioning_ovw2(en-us,VS.95).png
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker