CODEDIGEST
Home » Articles
Search
 

Technologies
 

Sponsored links
 

CodeDigest Navigation
 

Technology News
No News Feeds available at this time.
 

Community News
No News Feeds available at this time.
 
Silverlight 2 Controls – Overview

By Gaurav Arora
Posted On Apr 27,2009
Article Rating:
Be first to rate
this article.
No of Comments: 5
Category: Silverlight
Print this article.

Silverlight 2 Controls – Overview

 

In my previous article, we have learnt “How to start with Silverlight”. In the present article we will learn something about the available Silverlight Controls.

I am covering the entire Article in a practical view, so I advise you to look at my 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, let’s start to do some real work:

 

Silverlight Controls

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

 

Layout Controls

All layout controls derive from the basic abstract class Panel.

 

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

 

Canvas – It’s 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:

 

<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 : Canvas.” as Text.

 

Now let’s 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 can get confused by seeing the name with the ASP.Net DataGrid. But, it’s 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" />

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

        </StackPanel>

       

    </Grid>

  

</UserControl>

 

Above code will rearrange all rectangles horizontally.

 

Border – As the name suggest, 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:

 

<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="LayoutRoot" Background="White">

        <Border x:Name="thickBlackBrdr" BorderBrush="Black" BorderThickness="4"

                Width="200" Height="150">

        </Border>

    </Grid>

</UserControl>

 

Above code simply creates a Black Border with Thickness=4.

 

<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="LayoutRoot" Background="White">

        <Border x:Name="brdTest" BorderThickness="4" Width="200" Height="150">

            <Border.BorderBrush>

                <LinearGradientBrush x:Name="borderLinearGradientBrush" MappingMode="RelativeToBoundingBox"

                                     StartPoint="0.5,0" EndPoint="0.5,1">

                    <LinearGradientBrush.GradientStops>

                        <GradientStop Color="Yellow" Offset="0" />

                        <GradientStop Color="Blue" Offset="1" />

                    </LinearGradientBrush.GradientStops>

                </LinearGradientBrush>

            </Border.BorderBrush>

        </Border>

    </Grid>

 

</UserControl>

 

Try the above code and see the results.


Input Controls

For any project, the Input Controls are very essential and important. Let’s discuss some input controls:

 

Button and TextBox

 Button control is simple button to submit a form. With the Content property you can assign the text it to check the following code:

<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="LayoutRoot" Background="White">

        <Button x:Name="btnSubmit" Click="btnSubmit_Click"

            Content="Submit" Height="30" Width="75"

            HorizontalAlignment="Left"

            Margin="7,5,0,0"/>

    </Grid>

</UserControl>

 

On the other hand, TextBox is very common Input controls:

 

<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="LayoutRoot" Background="White">

        <TextBox x:Name="txtText" FontFamily="Arial"

            Width="200" Height="20" Margin="5" Text="I am the Text Box"

            HorizontalAlignment="Left" />       

    </Grid>

</UserControl>

 

Try the above code with different properties.

 

CheckBox and RadioButton

 These are common and an essential part of Silverlight application’s UI control set.

 

<UserControl xmlns:basics="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"  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">

    <Canvas  x:Name="LayoutRoot" Background="White">

        <CheckBox IsChecked="True" IsThreeState="True" Content="I am Checked" Canvas.Top="25" Canvas.Left="50" />

        <CheckBox IsChecked="False" IsThreeState="True" Content="Not Checked" Canvas.Top="50" Canvas.Left="50" />

        <RadioButton GroupName="rb" Content="Choose" Canvas.Top="75" Canvas.Left="50" />

        <RadioButton GroupName="rb" Content="Lets Choose Me" Canvas.Top="100" Canvas.Left="50" />

    </Canvas>

</UserControl>

 

The above code described the properties of Input Controls.

 

Calendar

 Calendar are another important Controls, these provide DateTime information.

 

<UserControl xmlns:basics="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"  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="LayoutRoot" Background="White">

        <basics:Calendar x:Name="myCal" DisplayMode="Month"

                FirstDayOfWeek="Sunday" Background="AliceBlue"  />

    </Grid>

</UserControl>

 

The Calendar control has a number of unique properties that customize the control's behavior, including AreDatesInPastSelectable,  DisplayDateEnd/Start,     DisplayMode,  FirstDayOfWeek,IsTodayHighlighted, and SelectableDateEnd/Start.

 

ListBox

It’s also another very important and flexible UI control. It is similar to the ASP.NET ListBox. This control accepts the number of items:

 

<UserControl xmlns:basics="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"  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">

    <Canvas  x:Name="LayoutRoot" Background="White">

        <ListBox Width="150" Height="80" Canvas.Top="50" Canvas.Left="50">

            <ListBoxItem Content="Introduction" />

            <ListBoxItem Content="Layout COntrols" />

            <ListBoxItem Content="Input COntrols" />

            <ListBoxItem Content="Defining Styles" />

            <ListBoxItem Content="Global Styles" />           

        </ListBox>

    </Canvas>

</UserControl>

 

Media Controls

Media controls contain Image and Media element controls.

It displays the image from specific path. Silverlight only supports JPEG and PNG images.

<UserControl xmlns:basics="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"  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">

    <Canvas  x:Name="LayoutRoot" Background="White">

          <Image x:Name="img" Source="images/myimage.png" />

    </Canvas>

</UserControl>

 

MediaElement

 It is used to display Video and audio as image displays only static images. The MediaElement only supports Windows Media Video, Windows Media Audio, and MP3 formats.

 

<UserControl xmlns:basics="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"  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">

    <Canvas  x:Name="LayoutRoot" Background="White">

        <MediaElement Source="WindowsMedia.wmv" AutoPlay="True" Opacity=".7">

        </MediaElement>

    </Canvas>

</UserControl>

 

Other Controls

 

DataGrid

 Datagrid is one of the essential UI controls. They radically simplify the task of displaying structured data to users by automatically handling the rendering of rows, columns, headers, and data navigation. Silverlight's data grid is no exception. While it is far from being a complete or "advanced" grid control by today's WinForms and ASP.NET standards, it does provide basic grid functionality.

 

To use the DataGrid control, you must simply bind the Grid to a list of items (that implement IEnumerable) via the ItemSource property. In the simplest approach, the Grid will automatically generate columns based on the data you supply and even render "special" column types- like checkbox columns- based on your data types. You can, of course, take more control and manually define the columns that will be rendered in your grid by setting the AutoGenerateColumns property to false.

 

We have to add following namespace to use DataGrid:

xmlns:my="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"

 

<my:DataGrid x:Name="myDataGrid" AutoGenerateColumns="true" />

 

Defining and using styles in Silverlight

Let’s take practical scenario for Styles in Silverlight.

Silverlight allows re-useable styles to be created to avoid duplication:

<Button x:Name="btnSubmit" FontFamily="Arial"

FontWeight="Bold" Width="100" Height="25"

Margin="10" />

 

<Button x:Name="btnCancel" FontFamily="Arial"

FontWeight="Bold" Width="100" Height="25"

Margin="10" />

 

We can also create Global Styles using app.xaml file

<Application.Resources>

<Style x:Key="ButtonStyle" TargetType="Button">

<Setter Property="Width" Value="100" />

</Style>

</Application.Resources>

 

Applying Styles to Controls

With the help of Style property we can apply the global style to the properties:

<Button x:Name="btnSubmit" Content="Submit"

Click="btnSubmit_Click"

Style="{StaticResource ButtonStyle}" />

 

Conclusion

Silverlight 2 provides us numerous controls which are described in this category, we have leave deep theoretical part because we will discuss all with full fledged example in coming articles. In this article we have the idea how Silverlight 2 controls behaves and what are the Controls of Silverlight2.

 

Similar Articles
You can contribute to CodeDiget.Com:
Donate to CodeDigest.com
Article Feedback
Comments
Wow, your post makes
Wow, your post makes mine look febeel. More power to you! http://qvkxcq.com [url=http://udbgthny.com]udbgthny[/url] [link=http://ofjkov.com]ofjkov[/link]
That <a href="http:/
That <a href="http://cuupbvgezga.com">ini'hgtss</a> just what I've been looking for. Thanks!
YMMD with that anres
YMMD with that anresw! TX http://mmpzzyutr.com [url=http://hnoiju.com]hnoiju[/url] [link=http://ekkbzuxm.com]ekkbzuxm[/link]
Thanks for the great
Thanks for the great info dog I owe you <a href="http://smgopkazltg.com">bigyitg.</a>
The hotnsey of your
The hotnsey of your posting is there for all to see