Skip to main content
Home Forums Silverlight Programming Silverlight Controls and Silverlight Toolkit ListBox: retrieve the SelectedItem
10 replies. Latest Post by Ravi.D on July 2, 2009.
(0)
Ultravi0let
Member
45 points
48 Posts
07-14-2008 6:38 AM |
Hello,
I'm listing some data from an SQL Database using LINQ to SQL and WCF (I followed Jesse Liberty's tutorial Displaying SQL Database Data in a DataGrid using LINQ and WCF to do it). My data are displayed in a LisBox (I get IDCategorie, LibelleCategorie . The ListBox shows the LibelleCategorie).
I created a SelectionChanged Event for my ListBox, and I need to retrieve the selected item, more specificaly the IDCategorie. How can I "read" MyListBox.SelectedItem ?
I'm using Silverlight 2 Beta 2, Visual Studio 2008.
Thanks in advance.
Regards,
Sanaa RAMZIJunior Developper
dharmesh...
83 points
30 Posts
07-14-2008 6:45 AM |
R u binding CategoryID column with any property of the listbox item. If not then bind CategoryID with the Tag property and in the SelectedChange Event, access the tag property to get the CategoryID.
07-14-2008 7:05 AM |
I added the Tag Property, here is my ListBox:
<ListBox x:Name="listCategories" VerticalAlignment="Top" HorizontalAlignment="Right" Height="100" Width="200" Grid.Row="2" Grid.Column="1" Margin="110,5,0,0" DisplayMemberPath="LibelleCategorie" Tag="IDCategorie" ></ListBox>
When I select a line in the ListBox, how do I get the value of this Tag property from the code behind?
07-14-2008 7:38 AM |
<ListBox x:Name="listCategories" VerticalAlignment="Top" HorizontalAlignment="Right" Height="100" Width="200" Grid.Row="2" Grid.Column="1" Margin="110,5,0,0" DisplayMemberPath="LibelleCategorie" Tag="IDCategorie" SelectionChanged="listCategories_SelectionChanged"></ListBox>
In code behind:
private void listCategories_SelectionChanged(object sender, SelectionChangedEventArgs e) { ListBox lb = (ListBox)sender; int id = (int)((object)lb.Items[lb.SelectedIndex]).IDCategorie; }Here object will be ur object with whom u r binding the listbox.
07-14-2008 7:51 AM |
I get this error:
'object' does not contain a definition for 'IDCategorie' and no extension method 'IDCategorie' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)
IDCategorie doesn't show when I use intellisense ..
lee_sl
Contributor
2990 points
584 Posts
07-14-2008 8:15 AM |
SelectedItem should give you the DataItem from which you can get all the properties
07-14-2008 8:18 AM |
First of all as i said did u change the (object) - with the List object that u r using to bind with ListBox. If not then try to do that first.
Or Else - Open ur application in debug mode and keep break point inside selectionchange event, open immediate window, try to get the value oflb.Items[lb.SelectedIndex] - it will return the List object that u have bind with ur listbox control. That obejct will contain the selected row value that is ur categoryid and categoryname. Or else:In ur first post u said u used linq to sql. Can u just copy paste the code that u have used to bind with the listbox control as well as the code that u have used for selectionchange event.
shiva.
160 points
50 Posts
07-14-2008 8:51 AM |
hai,
try this out.
<ListBox x:Name="lstCustomer" VerticalAlignment="Top" Margin="110,5,0,0" SelectionChanged="lstCustomer_SelectionChanged" DataContext="CustomerID" DisplayMemberPath="ContactName" HorizontalAlignment="Right" Height="100" Width="200" ></ListBox>
private void lstCustomer_SelectionChanged(object sender, SelectionChangedEventArgs e) { ServiceReference1.Customer oCustomer = (ServiceReference1.Customer)lstCustomer.SelectedItem;//cast the selected item object to ur class type string strCutomerId = oCustomer.CustomerID.ToString(); }
i have given example with Northwind.customer database.
Note: if this post has answered your question. plz mark it as answer
07-14-2008 9:32 AM |
YYESS !! this is exactly what I needed :) Thanks a lot ! I've been struggling with it for the past 3 days ! :)
Thank you all for yout time. Really appreciate it.
Best regards,
Sanaa
TWallace
10 points
5 Posts
07-25-2008 2:00 PM |
Thanks so much, Shiva! I had a similar problem and your post solved it for me.
Tom
Ravi.D
2 points
1 Posts
07-02-2009 9:32 AM |
Thanks Shiva. ! Been struggling for a couple of days also !
Thanks a lot !
Regards