Skip to main content

Microsoft Silverlight

Answered Question ListBox: retrieve the SelectedItemRSS Feed

(0)

Ultravi0let
Ultravi0let

Member

Member

45 points

48 Posts

ListBox: retrieve the SelectedItem

 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 RAMZI
Junior Developper

dharmesh.surti
dharmesh...

Member

Member

83 points

30 Posts

Re: ListBox: retrieve the SelectedItem

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.

 

Cheers,

Dharmesh Surti

Ultravi0let
Ultravi0let

Member

Member

45 points

48 Posts

Re: ListBox: retrieve the SelectedItem

 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?

dharmesh.surti
dharmesh...

Member

Member

83 points

30 Posts

Re: Re: ListBox: retrieve the SelectedItem

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

Cheers,

Dharmesh Surti

Ultravi0let
Ultravi0let

Member

Member

45 points

48 Posts

Re: Re: ListBox: retrieve the SelectedItem

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
lee_sl

Contributor

Contributor

2990 points

584 Posts

Re: Re: Re: ListBox: retrieve the SelectedItem

SelectedItem should give you the DataItem from which you can get all the properties

----------------------------------------------
Available for consulting in Dallas, TX
http://leeontech.wordpress.com/

dharmesh.surti
dharmesh...

Member

Member

83 points

30 Posts

Re: Re: Re: ListBox: retrieve the SelectedItem

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 of

lb.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.

Cheers,

Dharmesh Surti

shiva.
shiva.

Member

Member

160 points

50 Posts

Answered Question

Re: Re: ListBox: retrieve the SelectedItem

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 

Ultravi0let
Ultravi0let

Member

Member

45 points

48 Posts

Re: Re: ListBox: retrieve the SelectedItem

 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
TWallace

Member

Member

10 points

5 Posts

Re: Re: ListBox: retrieve the SelectedItem

Thanks so much, Shiva! I had a similar problem and your post solved it for me.

Tom

Ravi.D
Ravi.D

Member

Member

2 points

1 Posts

Re: Re: ListBox: retrieve the SelectedItem

Thanks Shiva. ! Been struggling for a couple of days also !

Thanks a lot !

Regards

 

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities