.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> Silverlight >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Silverlight Controls - A Step ahead Series

Posted By:Gaurav Arora       Posted Date: April 25, 2009    Points: 25    Category: Silverlight    URL: http://www.dotnetspark.com  
 

In my previous Articles we have learnt "How to start with Silverlight". In the present article we will learn Silverlight Controls.

I am covering the entire Article in a practical view so, I advise you please remind previous articles and create a test application for side-by-side practical or follow these steps to create a new test application:

 

  1. Select File -> New -> Project from Main Menu of Visual Studio
  2. Select C# / Visual Basic from the project types
  3. From Template list select Silverlight Template.
  4. Enter project name and location then create a new project [write here TestSilverlightControls].
  5. To host Silverlight within the project, select generate HTML test page.
  6. Finally click ok
  7. The page.xaml appears

 

Now, lets start to do some real work:

 

Silverlight Controls:

 

Silverlight Controls basically divided into: Layout Controls, Input Controls, Media Controls  and Other Controls etc.

 

 

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

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:

 

<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">

    <Canvas>

        <TextBlock Text="Testing Layout Control : Canvas."

            Canvas.Top="50" Canvas.Left="50" />

    Canvas>

UserControl>

 

Paste above the code in 'Page.xaml' in test project. This is simply display a TextBlock with : "Testing Layout Control : Canva" as Text.

 

Now lets do something extra with this:

 

Nested Canvas:

 

<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">

    <Canvas>

        <TextBlock Text="This is text on a blank canvas."

            Canvas.Top="50" Canvas.Left="50" />

        <Canvas Canvas.Top="50" Canvas.Left="50" Background="Red">

            <TextBlock Text="This is text on a blank canvas."

                  Canvas.Top="50" Canvas.Left="50" />

        Canvas>

    Canvas>

UserControl>

 

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:

 

<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">

    <Grid x:Name="myGrid" Background="White" >

        <Grid.ColumnDefinitions>

            <ColumnDefinition Width="*">ColumnDefinition>

            <ColumnDefinition Width="400">ColumnDefinition>

        Grid.ColumnDefinitions>

        <Grid.RowDefinitions>

            <RowDefinition Height="50">RowDefinition>

            <RowDefinition Height="100">RowDefinition>

            <RowDefinition Height="*">RowDefinition>

        Grid.RowDefinitions>

        <TextBlock Text="First Column of ROW1" Grid.Column="0" Grid.Row="0" />

        <TextBlock Text="Second Column ROW1" Grid.Column="1" Grid.Row="0" />

        <TextBlock Text="First Column of ROW2" Grid.Column="0" Grid.Row="1" />

        <TextBlock Text="Second Column of ROW2" Grid.Column="1" Grid.Row="1" />

        <TextBlock Text="First Column of ROW3" Grid.Column="0" Grid.Row="2" />

        <TextBlock Text="econd Column of ROW2" Grid.Column="1" Grid.Row="2" />

    Grid>

UserControl>

 

 

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:

 

<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">

    <Grid x:Name="myGrid" Background="White" >

        <StackPanel>

            <Rectangle Width="100" Height="50" Fill="Blue" />

            <Rectangle Width="75" Height="50" Fill="Red" />

            <Rectangle Width="50" Height="25" Fill="Pink" />

        StackPanel>

       

    Grid>

  

UserControl>

 

Note: By default orientation of StackPanel is Vertical

 

<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">

    <Grid x:Name="myGrid" Background="White" >

        <StackPanel Orientation="Horizontal" Background="LightGray"   >

            <Rectangle Width="100" Height="250" Fill="Blue" />

            <Rectangle Width="75" Height="175" Fill="Red" />

           

 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend