If you don't specify the position of an element, it is positioned
at the origin (0,0) of the display area. You can try this out yourself:
create a Silverlight XAML file and put some <TextBlock>
elements on it. The text contents within those elements will
overlap, since all text is displayed with the top-left corner at
(0,0).
You can change this for most elements by setting their Canvas.Top and Canvas.Left
properties. These properties denote the x and
y coordinates of the element, respectively. The
following text block would be shown 50 pixels to the right, 100 pixels to
the bottom:
<TextBlock Canvas.Left="50" Canvas.Top="100" Text="Silverlight" />
However, there is more to positioning, and here the
<Canvas> element comes into play again. A canvas can also have a
position:
<Canvas Canvas.Left="50" Canvas.Top="100">
...
</Canvas>
The clue is that all elements within the canvas
are positioned relative to the surrounding canvas. Have a look at Example 1, for instance.
Example 1. Nested, positioned canvases in the XAML file (Page.xaml, project
Canvas)
<UserControl x:Class="Canvas.Page" xmlns="http://schemas.microsoft.com/client/2007" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="400" Height="300"> <Grid x:Name="LayoutRoot" Background="White"> <Canvas Width="500" Height="500" Background="Red"> <Canvas Canvas.Left="50" Canvas.Top="50" Background="Green" Width="400" Height="400"> <Canvas Canvas.Left="50" Canvas.Top="50" Background="Yellow" Width="300" Height="300"> <Canvas Canvas.Left="50" Canvas.Top="50" Background="Blue" Width="200" Height="200"> <TextBlock Canvas.Left="50" Canvas.Top="50" FontSize="20" Text="Silverlight"/> </Canvas> </Canvas> </Canvas> </Canvas> </Grid> </UserControl>
|
It contains several canvases, each (except for the outer one) having
Canvas.Left="50" and Canvas.Top="50". Inside the
innermost <Canvas> element resides a <TextBlock> element with
Canvas.Left="50" and Canvas.Top="50" as well.
Each indentation always refers to the parent canvas and is not an absolute
coordinate. Therefore, each canvas starts 50 pixels to the right and 50
pixels to the bottom from where its parent canvas starts. The
Canvas.Left and Canvas.Top properties are also
called dependency properties: they
depend on their parent <Canvas> element. Likewise,
<Canvas> elements may be called dependency objects.
Figure 1 shows the browser output.