| Next Tip?
Home » Silverlight

What are Layout Controls in Silverlight2?

25 April 2009 146 views No Comment
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Layout Controls

All layout controls derive from the basic abstract class Panel.

It is clear from the name these controls define the layout. Silverlight2 proides several Layout Controls like :

Canvas – Its nothing but just similar to <div> tag of html. It exists from the first version of Silverlight and is a very basic Layout Control. It provides the basic facility to set the child elements using Canvas.Top, Canvas.Left properties.

Simple Canvas:
We can directly use the Layout control without specifying any other additional tasks:
&lt;UserControl x:Class="myFirstSilverlightApp.TestControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300"&gt;
&lt;Canvas&gt;
&lt;TextBlock Text="Testing Layout Control : Canvas."
Canvas.Top="50" Canvas.Left="50" /&gt;
&lt;/Canvas&gt;
&lt;/UserControl&gt;

Paste above the code in ‘Page.xaml’ in test project. This is simply display a TextBlock with : “Testing Layout Control : Canvas” as Text.

Now lets do something extra with this:

Nested Canvas:
&lt;UserControl x:Class="myFirstSilverlightApp.TestControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300"&gt;
&lt;Canvas&gt;
&lt;TextBlock Text="This is text on a blank canvas."
Canvas.Top="50" Canvas.Left="50" /&gt;
&lt;Canvas Canvas.Top="50" Canvas.Left="50" Background="Red"&gt;
&lt;TextBlock Text="This is text on a blank canvas."
Canvas.Top="50" Canvas.Left="50" /&gt;
&lt;/Canvas&gt;
&lt;/Canvas&gt;
&lt;/UserControl&gt;

Now, just look at above code, there are two Canvas used in above, in child canvas we have give the Top and Left values. Try the above code and see the results.

Grid– Yes, I can understand you are confused, I was too confused when I have read this word ‘Grid’ as a Asp.net guy, I was confused it with DataGrid. But, don’t worry Grid is not a DataGrid. Its another Layout Control just similar to Table of HTML. As a table Grid layout control provides the Row/ Column facility. One most exciting feature if Grid is it supports both fixed-width column and dynamic-width column. Its will be cleared from following example:
&lt;UserControl x:Class="myFirstSilverlightApp.TestControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="650" Height="300"&gt;
&lt;Grid x:Name="myGrid" Background="White" &gt;
&lt;Grid.ColumnDefinitions&gt;
&lt;ColumnDefinition Width="*"&gt;&lt;/ColumnDefinition&gt;
&lt;ColumnDefinition Width="400"&gt;&lt;/ColumnDefinition&gt;
&lt;/Grid.ColumnDefinitions&gt;
&lt;Grid.RowDefinitions&gt;
&lt;RowDefinition Height="50"&gt;&lt;/RowDefinition&gt;
&lt;RowDefinition Height="100"&gt;&lt;/RowDefinition&gt;
&lt;RowDefinition Height="*"&gt;&lt;/RowDefinition&gt;
&lt;/Grid.RowDefinitions&gt;
&lt;TextBlock Text="First Column of ROW1" Grid.Column="0" Grid.Row="0" /&gt;
&lt;TextBlock Text="Second Column ROW1" Grid.Column="1" Grid.Row="0" /&gt;
&lt;TextBlock Text="First Column of ROW2" Grid.Column="0" Grid.Row="1" /&gt;
&lt;TextBlock Text="Second Column of ROW2" Grid.Column="1" Grid.Row="1" /&gt;
&lt;TextBlock Text="First Column of ROW3" Grid.Column="0" Grid.Row="2" /&gt;
&lt;TextBlock Text="econd Column of ROW2" Grid.Column="1" Grid.Row="2" /&gt;
&lt;/Grid&gt;
&lt;/UserControl&gt;

Dynamic-Width can be assigned with the use of ‘*’
Grid provides some more extra feature like spanning and others same as HTML Table.

StackPanel– It provides the facility to arrange elements both Horizontally and vertically.
Now, suppose we have to draw three rectangles vertically, if you chose other layout controls then all rectangles will be overlapped [try the same] but with the help of StackPanel its not:
&lt;UserControl x:Class="myFirstSilverlightApp.TestControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="650" Height="300"&gt;
&lt;Grid x:Name="myGrid" Background="White" &gt;
&lt;StackPanel&gt;
&lt;Rectangle Width="100" Height="50" Fill="Blue" /&gt;
&lt;Rectangle Width="75" Height="50" Fill="Red" /&gt;
&lt;Rectangle Width="50" Height="25" Fill="Pink" /&gt;
&lt;/StackPanel&gt;

&lt;/Grid&gt;

&lt;/UserControl&gt;
Note: By default orientation of StackPanel is Vertical
&lt;UserControl x:Class="myFirstSilverlightApp.TestControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="650" Height="300"&gt;
&lt;Grid x:Name="myGrid" Background="White" &gt;
&lt;StackPanel Orientation="Horizontal" Background="LightGray" &gt;
&lt;Rectangle Width="100" Height="250" Fill="Blue" /&gt;
&lt;Rectangle Width="75" Height="175" Fill="Red" /&gt;
&lt;Rectangle Width="50" Height="125" Fill="Pink" /&gt;
&lt;/StackPanel&gt;

&lt;/Grid&gt;

&lt;/UserControl&gt;

Above code rearrange all rectangles horizontally.

Border – As it pronounced from name, it creates a border. Any control can put in the border and it creates just a border for its child controls, lets check some taste of examples:
&lt;UserControl x:Class="myFirstSilverlightApp.TestControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="650" Height="300"&gt;
&lt;Grid x:Name="LayoutRoot" Background="White"&gt;
&lt;Border x:Name="thickBlackBrdr" BorderBrush="Black" BorderThickness="4"
Width="200" Height="150"&gt;
&lt;/Border&gt;
&lt;/Grid&gt;

&lt;/UserControl&gt;

Above code simply creates a Black Border with Thickness=4.
&lt;UserControl x:Class="myFirstSilverlightApp.TestControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="650" Height="300"&gt;
&lt;Grid x:Name="LayoutRoot" Background="White"&gt;
&lt;Border x:Name="brdTest" BorderThickness="4" Width="200" Height="150"&gt;
&lt;Border.BorderBrush&gt;
&lt;LinearGradientBrush x:Name="borderLinearGradientBrush" MappingMode="RelativeToBoundingBox"
StartPoint="0.5,0" EndPoint="0.5,1"&gt;
&lt;LinearGradientBrush.GradientStops&gt;
&lt;GradientStop Color="Yellow" Offset="0" /&gt;
&lt;GradientStop Color="Blue" Offset="1" /&gt;
&lt;/LinearGradientBrush.GradientStops&gt;
&lt;/LinearGradientBrush&gt;
&lt;/Border.BorderBrush&gt;
&lt;/Border&gt;
&lt;/Grid&gt;

&lt;/UserControl&gt;
Try above code and see the results.

Popularity: 1%

Post to Twitter Tweet This Post

Related posts:

  1. Using Combobox in Silverlight – in a smarter way After reading this article we will be able to use...
  2. File Handling in Silverlight ...
  3. Isolated Storage in the Silverlight Application Isolated Storage Isolated Storage in the Silverlight Application Definition: Isolated...

Related posts brought to you by Yet Another Related Posts Plugin.

Ads by Lake Quincy Media

Leave your response!

You must be logged in to post a comment.

Get Adobe Flash playerPlugin by wpburn.com wordpress themes