SIGN UP MEMBER LOGIN:    
ARTICLE

List Box in Silverlight 3 Application

Posted by Diptimaya Patra Articles | XAML with C# July 28, 2009
In this article we will explore what are features available for a ListBox in Silverlight 3 Application.
Reader Level:
Download Files:
 

Introduction

In this article we will explore what are features available for a ListBox in Silverlight 3 Application.

Crating Silverlight Project

Fire up Visual Studio 2008 and create a Silverlight Application. Name it as ListBoxInSL3.

image1.gif

Here is the basic idea what we are going to do in this Silverlight Application: We will add ListBox and a TextBlock, whatever item you select from the ListBox it will be displayed in the TextBox.

Open Expression Blend 3 and start designing the application. Follow the below figure to have better idea.

image2.gif

If you look at the Objects and Timeline Pane you will find these following controls are added with specific names.

image3.gif

In ListBox we are going to explore the property called SelectionMode. If you want to see it, just select ListView and in the Properties Pane in Common Properties you will find it.

image4.gif

Selection Mode has three modes:

  • Single
  • Multiple
  • Extended

Now open the solution in Visual Studio 2008, we have code to write. J
We will use the Multiple Selection Mode for the ListBox.

<ListBox x:Name="MyListBox" Height="100" SelectionMode="Multiple" HorizontalAlignment="Left" VerticalAlignment="Top" Width="100" Margin="8,39,0,0"/>

Add a List of strings as the Item Source of the ListBox.

List<string> myList = new List<string> { "C", "C++", "C#", "Java", "Visual Basic", "XAML" };
public MainPage()
        {
            InitializeComponent();
            MyListBox.ItemsSource = myList;
        }

Now we will add an event to the ListBox, called SelectionChanged.

<ListBox x:Name="MyListBox" Height="100" SelectionMode="Multiple" HorizontalAlignment="Left" VerticalAlignment="Top" Width="100" Margin="8,39,0,0" SelectionChanged="MyListBox_SelectionChanged"/>

Now in MainPage.xaml.cs find the event handler method and add the following code, to make sure what are the selections we have made.

private void MyListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var selectedExp = MyListBox.SelectedItems;
            Experience.Text = string.Empty;
            foreach (string langName in selectedExp)
            {
                if (Experience.Text.Length>0)
                {
                    Experience.Text += ", ";
                }
                Experience.Text += langName;
            }
        }

Now Run your application and select multiple values from the ListBox and you will find the selections are added to the TextBlock.

image5.gif

Single Mode

Now you might have thought of using the other two Modes: Single and Extended.

Single, as the name says you will be able to select only a single item in the ListBox.

What is Extended Mode?

It's nothing but the combination of both Single Mode and Multiple Mode. If you normally select it will be Single select mode and if you Press CTRL and select it will be Multiple mode.

Go ahead and try for it.

Enjoy Coding.

Login to add your contents and source code to this article
share this article :
post comment
 

Hi,
How can you implement a 4th selection mode? For example, if I will click on item 1 and hold the mouse down and drag the mouse to item 4 and then release the mouse button, and then see item 1 to 4 selected.
So we are talking about a Drag Selection mode. How can I do that?
Regards

Posted by Giorgio Nov 25, 2009

Hi, first thanks the sharing. but I think you had forget to consider one case, when user's selection collection changed, the new selected collection will be shown after the the previous one. Duplication may occur.

Posted by Lisa Aug 14, 2009

Sir ,
you have send me a  2 options about error -
one that i install silver light 3 new version but it did not work
i got same error message as -
silver light error message
error code  4005
error type parse error
4005 an error has occured
xamlfile :http://localhost:4163/silverlightproject2/page.xaml
line 5
position 17
 

send one options -
to install fresh silverlight tools u send me vs2008


Posted by praval singh Jul 30, 2009

Hi Praval,

you are getting error because of the Silverlight 3 version. Try to install a newer version. You will get the latest version of runtime in the following link:
http://silverlight.net/getstarted/silverlight3/default.aspx

I have downloaded the Sample that I have provided; into my machine and its running good.

Try to install in the following order:
1) Silverlight 3 Runtime
2) Silverlight 3 Tool for VS 2009
3) Silverlight 3 Toolkit July 2009 Release
4) Expression Blend 3 + Sketch (Optional for Designing)

Thanks
Diptimaya Patra

Posted by Diptimaya Patra Jul 30, 2009

Sir,
thank for send me a message as version problem
but download your listbox application and successfully run in browser
still now i create a new silverlight application it gives me error
silverlight error message
error code 4005
silverlight parse error

praval_pr@yahoo.com

Posted by praval singh Jul 30, 2009
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.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Nevron Gauge for SharePoint
Become a Sponsor