Layout in XAML
page 7 of 8
by Keyvan Nayyeri
Feedback
Average Rating: 
Views (Total / Last 10 Days): 28365/ 269

Canvas

Canvas is a solution for absolute positioning in XAML.  Canvas has four Attached Properties in its child elements: Canvas.Top, Canvas.Left, Canvas.Right and Canvas.Bottom.

These attributes specify the distance between the top left corner of an element and its parent Canvas.  Obviously, by setting one of Canvas.Top and Canvas.Bottom and one of Canvas.Left and Canvas.Right, you will get the result and do not need to set others.  However, if you set both attributes, Canvas.Top and Canvas.Left are preferred.  Also, you can set only one of these attributes for an element.

Listing 11 and Figure 11 help you to see the concept of Canvas in a simple example.

Listing 11

<Window x:Class="LayoutinXAML.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Layout in XAML" Height="300" Width="300"
    >
  <Canvas>
    <TextBox Background="Coral" Grid.Row="0" Grid.Column="0" 
             Canvas.Top="10" Canvas.Left="20">
      ASP Alliance 1
    </TextBox>
    <TextBox Background="Plum" Grid.Row="0" Grid.Column="1" 
             Canvas.Bottom="60" Canvas.Left="40">
      ASP Alliance 2
    </TextBox>
    <TextBox Background="Aquamarine" Grid.Row="1" Grid.Column="0"
             Canvas.Top="80" Canvas.Right="110">
      ASP Alliance 3
    </TextBox>
    <TextBox Background="Teal" Grid.Row="1" Grid.Column="1"
              Canvas.Bottom="95" Canvas.Right="30">
      ASP Alliance 4
    </TextBox>
  </Canvas>
</Window>

Figure 11

I said Canvas Attached Properties have relative values to their parent Canvas elements.  So if you nest a Canvas element within another and declare some elements in second Canvas, those elements will get their position from their direct parent Canvas.  Listing 12 and Figure 12 show this concept.

Listing 12

<Window x:Class="LayoutinXAML.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Layout in XAML" Height="300" Width="300"
    >
  <Canvas>
    <TextBox Background="Coral" Grid.Row="0" Grid.Column="0" 
             Canvas.Top="10" Canvas.Left="20">
      ASP Alliance 1
    </TextBox>
    <TextBox Background="Plum" Grid.Row="0" Grid.Column="1" 
             Canvas.Top="60" Canvas.Left="40">
      ASP Alliance 2
    </TextBox>
    <Canvas Canvas.Top="120" Canvas.Left="140">
      <TextBox Background="Aquamarine" Grid.Row="1" Grid.Column="0"
         Canvas.Top="5" Canvas.Left="10">
        ASP Alliance 3
      </TextBox>
      <TextBox Background="Teal" Grid.Row="1" Grid.Column="1"
                Canvas.Top="60" Canvas.Left="50">
        ASP Alliance 4
      </TextBox>
    </Canvas>
  </Canvas>
</Window>

Figure 12


View Entire Article

Article Feedback

Title:  
Name:  
Url: ( Optional )
Comment:  
Please add 1 and 8 and type the answer here:

User Comments

Title: Complement   
Name: Mehta Chandni K.
Date: 2/9/2009 4:08:59 AM
Comment:
A very good article. I got to learn many basic and important things on XAML. Thank you very much. Keep uploading such articles for students like us..
Title: XAML Layout   
Name: Anjan
Date: 5/15/2007 6:53:25 AM
Comment:
It is fine with windows.I want to Layout the Page with WINDOWS VISTA based.
Title: Events in Windows Presentation Foundation   
Name: Keyvan Nayyeri
Date: 12/14/2006 10:22:47 AM
Comment:
Fifth part of these tutorials about Events in Windows Presentation Foundation:
http://aspalliance.com/1088_Events_in_Windows_Presentation_Foundation
Title: Correction   
Name: Keyvan Nayyeri
Date: 10/27/2006 8:36:05 AM
Comment:
Actually nothing :-D

I just forgot to remove them from previous code but they won't affect the result anyways.

Thanks for pointing.
Title: grid...   
Name: Amirhossein Babaeian
Date: 10/27/2006 6:46:47 AM
Comment:
what's the roll of grid.row and grid.column in controls that contains in cansvas tags?
Title: Animations in XAML   
Name: Keyvan Nayyeri
Date: 10/25/2006 12:36:20 AM
Comment:
Fourth part of these tutorials about Animations in XAML:
http://aspalliance.com/1050_Animations_in_XAML
Title: Resources in XAML   
Name: Keyvan Nayyeri
Date: 10/12/2006 12:58:22 AM
Comment:
Third part of these tutorials about Resources in XAML:
http://aspalliance.com/1032_Resources_in_XAML
Title: Nice article   
Name: Harry
Date: 10/10/2006 12:27:22 AM
Comment:
Hi Keyvan,
This is very nice article about XAML. I read your previous article also, even that was alos good for begginers.

Keep it up....

Harry
Title: Introduction to XAML   
Name: Keyvan Nayyeri
Date: 10/9/2006 2:08:59 PM
Comment:
First part of these tutorials, Introduction to XAML:
http://aspalliance.com/1019_Introduction_to_XAML

Product Spotlight
Product Spotlight 






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2009 ASPAlliance.com  |  Page Processed at 11/20/2009 11:07:56 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search