Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I know this may sound silly but took me a few hours and couldn't get it right.
How to get selecteditem from the silverlight listbox?
VB
Dim item As Object
For Each item In Me.ListBox1.SelectedItems
//here i just could not do it.
Next


This is the service that populate the listbox
VB
Dim proxy As New ...()
AddHandler proxy.GetDistrictCompleted, AddressOf Proxy_GetDistrictCompleted
proxy.GetDistrictAsync()

Me.ListBox1.ItemsSource = e.Result
Me.ListBox1.DisplayMemberPath = "District"
Me.ListBox1.SelectedValuePath = "District"

I know inside out on the asp but silverlight seems to be different.
Posted
Updated 27-Jan-11 6:56am
v2

Just cast item to your specific data class type.

I am not sure of the VB syntax for this, but in C# you'd do this:

C#
MyType myType = (MyType)item;
DoStuff(myType.MyProperty, etc...);
 
Share this answer
 
v4
Comments
Espen Harlinn 27-Jan-11 13:49pm    
5+ TryCast(VB.Net) ... away?? :)
bobbymale77 27-Jan-11 15:26pm    
The item only showing item.toString which gives me the dataproperty and not the datavalue.
Try something like:
VB
Dim lbi As ListBoxItem = TryCast(TryCast(sender, ListBox).SelectedItem, ListBoxItem)
textBlock1.Text = " You selected " + lbi.Content.ToString() + "."


Have a look at full detail with sample here[^].
 
Share this answer
 
Comments
bobbymale77 27-Jan-11 15:25pm    
I dont have the content. The listbox is bound to a data property called "District".

Dim item As Object

For Each item In Me.ListBox1.SelectedItems

Dim district as string = Me.ListBox1.SelectedValue
This will show for the first selected value I need the item. Why can't I used item.toString()?

Next

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900