List Box Control In Silverlight 3 Application

No.of Views1568
Bookmarked0 times
Downloads 
Votes0
By  dpatra   On  16 Feb 2010 00:02:01
Tag : SharePoint , Development and Programming
List Box Control In Silverlight 3 Application
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

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

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

Image Loading...


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.


Image Loading

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

Image Loading...

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.

Image Loading..

Selection Mode has three modes:

  • Single
  • Multiple
  • Extended

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

{codecitation class="brush: xml; gutter: true;" width="650px"}

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

{/codecitation}
Add a List of strings as the Item Source of the ListBox.

{codecitation class="brush: csharp; gutter: true;" width="650px"}

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




{/codecitation}

Now we will add an event to the ListBox, called SelectionChanged.
{codecitation class="brush: xml; gutter: true;" width="650px"}

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

{/codecitation}

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.

{codecitation class="brush: csharp; gutter: true;" width="650px"}

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;
}
}


{/codecitation}

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

Image Loading....

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.Thank you


About the Author


Diptimaya Patra

Description :I am a Master in Computer Application (MCA) from SRM University, Chennai. I am MCTS in ASP.Net Web Development, and MOSS 2007 Administration. I have extreme exposure to Microsoft Technologies in recent times like Silverlight 2, Silverlight 3. I am from Cuttack, Orissa. You can reach me using this mail (diptimaya.patra@gmail.com). Currently I am working as a Software Engineer in UST Global Inc in Trivandrum Center.

Occupation :Software Engineer
Company : UST Global.
Location : India
Follow me at twitter : http://twitter.com/dpatra


 
Sign Up to vote for this article
 
About Author
 
dpatra
Occupation-Not Provided
Company-Not Provided
Member Type-Expert
Location-Not Provided
Joined date-13 Jul 2009
Home Page-Not Provided
Blog Page-Not Provided
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top