(X) Hide this
    • Login
    • Join
      • Generate New Image
        By clicking 'Register' you accept the terms of use .

Using the Canvas control in Silverlight

(3 votes)
Nikolay Raychev
>
Nikolay Raychev
Joined Mar 28, 2008
Articles:   22
Comments:   58
More Articles
14 comments   /   posted on Apr 07, 2008
Tags:   canvas , layout , nikolay-raychev
Categories:   Controls

This article is compatible with the latest version of Silverlight.

Introduction

The Canvas is the simplest layout control used as a container for other Silverlight controls. The inner controls are positioned absolutely towards its left and top sides.

See also:
Silverlight Layout controls
Grid Article
StackPanel Article

Overview

The following example demonstrates the use of the Canvas:

We want to have a rectangle positioned 100 pixels from the left side and 50 pixels from the top side:

The child controls of the Canvas are positioned absolutely towards its left and top sides by their “Canvas.Left” and “Canvas.Top” attached properties.

Here is the XAML code:

<UserControl x:Class="Canvas2.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="400" Height="300">
    <Canvas x:Name="cnvControlHolder" Background="White" Height="300" Width="400">
        <Rectangle x:Name="rectTest" Canvas.Left="100" Canvas.Top="50" Fill="Blue" Width="60" Height="40"></Rectangle>
    </Canvas>
</UserControl>

 

Other important property which you can set to the controls inside the canvas is the Canvas.ZIndex. just like the z-index in CSS. Here is a little example:

We want to have a button in front of a rectangle:

<Canvas x:Name="cnvControlHolder" Background="White" Height="300" Width="400">
    <Rectangle x:Name="rectTest" Canvas.Left="100" Canvas.Top="50" Canvas.ZIndex="0" Fill="Blue" Width="60" Height="40"></Rectangle>
    <Button x:Name="btnTest" Canvas.Left="120" Canvas.Top="70" Canvas.ZIndex="1" Content="Button :)"></Button>
</Canvas>

We have a button with Canvas.ZIndex="1" that is above our Rectangle because its z index is bigger. There is nothing you can misunderstand.

You can get or set the “Canvas.Left”, “Canvas.Top” and “Canvas.ZIndex” attached properties programatically using the static methods of the Canvas class.

The following example demonstrates it:

Code behind:

public partial class Page : UserControl
{
    public Page()
    {
        InitializeComponent();
 
        Canvas.SetTop( this.rectTest, 80 );
        Canvas.SetLeft( this.rectTest, 130 );
        Canvas.SetZIndex( this.rectTest, 2 );
    }
}

Result:

Here is another example where we get the three properties and increase them:

Canvas.SetTop( this.rectTest, Canvas.GetTop( this.rectTest ) + 30 );
Canvas.SetLeft( this.rectTest, Canvas.GetLeft( this.rectTest ) + 30 );
Canvas.SetZIndex( this.rectTest, Canvas.GetZIndex( this.rectTest ) + 2 );

The result is the same.

Conclusion

This article is a brief description of the key features of the Canvas control. It targets the developer who has just started with the Silverlight controls. Any comments are welcome.

Reference

http://silverlight.net/learn/tutorials.aspx
http://msdn2.microsoft.com/en-us/library/system.windows.controls.canvas(VS.95).aspx


Subscribe

Comments

  • -_-

    RE: Using the Canvas control in Silverlight 2


    posted by Joe on Apr 09, 2008 18:56

    How about what happens if you use a Canvas without specifying the attached properties and use HorizontalAlignment in the child element? Also, how does a canvas deal with the Measure/Arrange process when it is being layed out by a parent panel?

  • nikolayraychev

    RE: Using the Canvas control in Silverlight 2


    posted by nikolayraychev on Apr 10, 2008 06:15

    Hi Joe,

    Using the FrameworkElement.HorizontalAlignment property in the child element will not work with the Canvas. This is mentioned here:

    http://msdn2.microsoft.com/en-us/library/system.windows.frameworkelement.horizontalalignment(VS.95).aspx

    You can use a Grid to achieve a horizontal or a vertical alignment.

    Regarding the second question I wrote a small article about it:

    http://www.silverlightshow.net/items/How-does-the-Canvas-control-deal-with-the-Measure-Arrange-process.aspx

  • -_-

    RE: Using the Canvas control in Silverlight 2


    posted by ben on Apr 18, 2008 03:00

    it is perfect ,thanks a lot! it does me a big favor.

  • -_-

    RE: Using the Canvas control in Silverlight 2


    posted by shiva on Jul 29, 2009 16:02
    what is the difference between grid and canvas control
  • nikolayraychev

    RE: Using the Canvas control in Silverlight 2


    posted by nikolayraychev on Jul 30, 2009 10:46

    Hi Shiva,

    Here you can see my article about the Grid:

    http://www.silverlightshow.net/items/Using-the-Grid-control-in-Silverlight-2-Beta-1.aspx

    Infact everything is different :)

  • -_-

    RE: Using the Canvas control in Silverlight 2


    posted by Shiva on Aug 03, 2009 11:12
    Hi nicko,
             I have already check ur grid control........Its really nice.........I want  wat is the difference bw grid and canvas control
  • nikolayraychev

    RE: Using the Canvas control in Silverlight 2


    posted by nikolayraychev on Aug 12, 2009 10:04
    The main difference is that the inner controls in the Canvas are positioned absolutely towards its left and top sides and in the Grid they are positioned relatively.
  • -_-

    RE: Using the Canvas control in Silverlight 2


    posted by Satish Verma on Nov 11, 2009 09:27
    thanks.
  • -_-

    RE: Using the Canvas control in Silverlight 2


    posted by Satish Verma on Nov 11, 2009 09:28
    it is perfect ,thanks a lot!
  • -_-

    RE: Using the Canvas control in Silverlight 2


    posted by Kamal on Jan 11, 2010 23:28
    Thanks for the write. Just what I was looking for. Works with Silverlight 3.
  • -_-

    RE: Using the Canvas control in Silverlight 2


    posted by Amir on Apr 18, 2010 03:24

    Hi,

    What is the difference between margins and Canvas.Left/Top?

    Thank you

  • nikolayraychev

    RE: Using the Canvas control in Silverlight 2


    posted by nikolayraychev on Apr 20, 2010 10:57

    Hi Amir,

    if you mean margins of a child element of the Canvas you can achieve the same effect with Canvas.Left/Top.

  • -_-

    RE: Using the Canvas control in Silverlight 2


    posted by Kareem on Apr 24, 2010 20:28
    Thank you.
  • lnikolov

    RE: Using the Canvas control in Silverlight


    posted by lnikolov on Jan 03, 2011 10:08
    The article has been updated to the latest version of Silverlight and Visual Studio.

Add Comment

Login to comment:
  *      *