SIGN UP MEMBER LOGIN:    
ARTICLE

Working with Canvas in XAML Silverlight

Posted by Abhimanyu Kumar Vatsa Articles | Silverlight with C# April 11, 2011
In this article, you will learn how to work with Canvas as well as its properties.
Reader Level:


Introduction

The entire Silverlight application involves properties and events. We need to set-up the properties of an element and attach an event handler to their events to play. Ok...let's talk about the topic of a Canvas and it's properties. First the definition; what is a Canvas?

We may define it as a container of many child elements such as rectangles, text blocks etc. but remember it can't contain another canvas. Look at the screenshot below; it has five rectangles and each of them is aligned through its Canvas.Left and Canvas.Top properties.

XAMLCanvas1.gif

<UserControl
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          x:Class="SilverlightApplication1.MainPage"
          Width="304" Height="304">

          <Canvas>

                   <Rectangle
                   Canvas.Left="2" Canvas.Top="2"
                   Height="100" Width="100"
                   Stroke="Green"
                             StrokeThickness="5"
                             Fill="Red"/>

                   <Rectangle
                   Canvas.Left="102" Canvas.Top="102"
                   Height="100" Width="100"
                   Stroke="Yellow"
                             StrokeThickness="5"
                             Fill="White"/>

                   <Rectangle
                   Canvas.Left="202" Canvas.Top="202"
                   Height="100" Width="100"
                   Stroke="Green"
                             StrokeThickness="5"
                             Fill="Red"/>

                   <Rectangle
                   Canvas.Left="202" Canvas.Top="2"
                   Height="100" Width="100"
                   Stroke="Green"
                             StrokeThickness="5"
                             Fill="Red"/>

                   <Rectangle
                   Canvas.Left="2" Canvas.Top="202"
                   Height="100" Width="100"
                   Stroke="Green"
                             StrokeThickness="5"
                             Fill="Red"/>

          <Canvas.Background>
          <ImageBrush ImageSource="image.jpg" />
          </Canvas.Background>

          </Canvas>

</UserControl>


The above canvas has one attribute:

  • Background: It is a brush; in this case we use a plain 'Magenta' color to paint the background of the Canvas.

    The above canvas also has five child elements:

  • Rectangle: It is simply a shape that can be drawn inside a canvas; it can't exist without a container. It also has the following attributes:

    --> Canvas.Left or Canvas.Top: It specifies the position of the control with respect to the Canvas location. Canvas.Left="2" means leave 2 pixels from the left and Canvas.Top="2" means leave 2 pixels from the top. If we apply the same margin for another control then it will overlap one another; look at below:

    XAMLCanvas2.gif

Canvas Background Implementation

A Canvas is a container so we can apply images or gradient color as its background. Let's apply image to its background. If you wish to apply an image, add the image to the Project Explorer. Remember to upload image in Silverlight Application root not in Silverlight Application Website root. Let's apply the image.

XAMLCanvas3.gif

<UserControl
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          x:Class="SilverlightApplication1.MainPage"
          Width="304" Height="304">

          <Canvas>

                   <Rectangle
                   Canvas.Left="2" Canvas.Top="2"
                   Height="100" Width="100"
                   Stroke="Green"
                             StrokeThickness="5"
                             Fill="Red"/>

                   <Rectangle
                   Canvas.Left="102" Canvas.Top="102"
                   Height="100" Width="100"
                   Stroke="Yellow"
                             StrokeThickness="5"
                             Fill="White"/>

                   <Rectangle
                   Canvas.Left="202" Canvas.Top="202"
                   Height="100" Width="100"
                   Stroke="Green"
                             StrokeThickness="5"
                             Fill="Red"/>

                   <Rectangle
                   Canvas.Left="202" Canvas.Top="2"
                   Height="100" Width="100"
                   Stroke="Green"
                             StrokeThickness="5"
                             Fill="Red"/>

                   <Rectangle
                   Canvas.Left="2" Canvas.Top="202"
                   Height="100" Width="100"
                   Stroke="Green"
                             StrokeThickness="5"
                             Fill="Red"/>
                    <Canvas.Background>
                             <RadialGradientBrush>
                                      <GradientStop Color="Orange" Offset="0.22"/>
                                      <GradientStop Color="Green" Offset="0.959"/>
                                      <GradientStop Color="Orange" Offset="0.588"/>
                             </RadialGradientBrush>
                   </Canvas.Background>

          </Canvas>

</UserControl>

Remember to delete background color before applying image because there is only single scope of background either use it by image or color/gradient.

Now, let's apply a gradient to the Canvas but delete the image before proceeding.

XAMLCanvas4.gif

I accomplish this using the Properties Explorer of the Canvas and the generated code is as follows:

<UserControl
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          x:Class="SilverlightApplication1.MainPage"
          Width="304" Height="304">

          <Canvas>

                   <Rectangle
                   Canvas.Left="2" Canvas.Top="2"
                   Height="100" Width="100"
                   Stroke="Green"
                             StrokeThickness="5"
                             Fill="Red"/>

                   <Rectangle
                   Canvas.Left="102" Canvas.Top="102"
                   Height="100" Width="100"
                   Stroke="Yellow"
                             StrokeThickness="5"
                             Fill="White"/>

                   <Rectangle
                   Canvas.Left="202" Canvas.Top="202"
                   Height="100" Width="100"
                   Stroke="Green"
                             StrokeThickness="5"
                             Fill="Red"/>

                   <Rectangle
                   Canvas.Left="202" Canvas.Top="2"
                   Height="100" Width="100"
                   Stroke="Green"
                             StrokeThickness="5"
                             Fill="Red"/>

                   <Rectangle
                   Canvas.Left="2" Canvas.Top="202"
                   Height="100" Width="100"
                   Stroke="Green"
                             StrokeThickness="5"
                             Fill="Red"/>

                   <Canvas.Background>
                             <RadialGradientBrush>
                                      <GradientStop Color="Orange" Offset="0.22"/>
                                      <GradientStop Color="Green" Offset="0.959"/>
                                      <GradientStop Color="Orange" Offset="0.588"/>
                             </RadialGradientBrush>
                   </Canvas.Background>

          </Canvas>

</UserControl>

That's all about the Canvas; be tuned for the next article.

HAVE A GREAT CODING
 

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Nevron Gauge for SharePoint
Become a Sponsor