ARTICLE

ListBox control in Silverlight using VB.NET

Posted by Rohatash Kumar Articles | Silverlight using VB.NET December 22, 2010
In this article we will learn how to use ListBox control in Silverlight 4.
 
Reader Level:

In this article we will learn how to use ListBox control in Silverlight 4.

ListBox control

The Silverlight ListBox control provides all the functionality of a standard (web) ListBox.

The ListBox tag represents a Silverlight ListBox control in XAML.

 

<ListBox></ListBox>

 

The Width and Height attributes represent the width and the height of a ListBox.  The Control can be uniquely identified by x:Name attribute represents the name of the control.

 

The following XAML code sets the name, height and width  of ListBox control.

 

<ListBox Height="100" HorizontalAlignment="Left" Margin="10,10,0,0" Name="ListBox1" VerticalAlignment="Top" Width="120" />

Properties - ListBox control has the following properties.

tb1.gif

Figure 1.

Items - Items collection property is used to add the items in the ListBox control.

Foreground - The Foreground attributes of ListBoxItem represents the foreground colors of the item.

Background -  The Background attributes of ListBoxItem represents the background colors of the item.

Adding ListBox Items

To add ListBox items in the ListBox using items property of the ListBox control.

tb2.gif

Figure 2.

XAML code

<ListBox Height="100" HorizontalAlignment="Left" Margin="10,10,0,0" Name="ListBox1" VerticalAlignment="Top" Width="120">

            <ListBoxItem Content="Cricket" />

            <ListBoxItem Content="Football" />

            <ListBoxItem Content="Hockey" />

            <ListBoxItem Content="Tenis" />

        </ListBox>

 

Formatting ListBox Items

 

Select every items in the ListBox and set the foreground and background property.


tb3.gif 

Figure 3.

 

XAML code

 

<ListBox Height="100" HorizontalAlignment="Left" Margin="10,10,0,0" Name="ListBox1" VerticalAlignment="Top" Width="120">

            <ListBoxItem Content="Cricket" Background="SpringGreen" Foreground="Blue" />

            <ListBoxItem Content="Football" Background="LightPink" Foreground="Blue" />

            <ListBoxItem Content="Hockey" Foreground="Green" Background="Yellow" />

            <ListBoxItem Content="Tenis" Background="Red" Foreground="BlueViolet" />

        </ListBox>

 

The form looks like this.


tb4.gif 

Figure 4.

 

Displaying Images in a ListBox

 

Taking an Image control on the form and apply source property of image control.


tb5.gif 

Figure 5.

 

The form looks like this.


tb6.gif 

Figure 6.

 

XAML code

 

<ListBox Height="110" HorizontalAlignment="Left" Margin="10,10,0,0" Name="ListBox1" VerticalAlignment="Top" Width="255" Background="SpringGreen">

            <ListBoxItem Content="        Cricket" Foreground="Blue">

                <ListBoxItem.Background>

                    <ImageBrush />

                </ListBoxItem.Background>

            </ListBoxItem>

            <ListBoxItem Content="Football" Background="LightPink" Foreground="Blue" />

            <ListBoxItem Content="Hockey" Foreground="Green" Background="Yellow" />

            <ListBoxItem Content="Tenis" Background="Red" Foreground="BlueViolet" />

        </ListBox>

        <Image Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="Image1" Stretch="Fill" VerticalAlignment="Top" Width="26" Source="/SilverlightApplication45;component/Images/imagec26.jpg" />

        <Grid.Background>

            <ImageBrush />

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Become a Sponsor