Skip to main content
Home Forums Silverlight Programming Silverlight Controls and Silverlight Toolkit Newbie question about the listbox
13 replies. Latest Post by SilverlightShow on September 4, 2008.
(0)
olliraa
Member
5 points
35 Posts
09-02-2008 8:20 AM |
Could someone please give me an example on how do I make the listbox work? I mean, If I have 3 items (item1, item2 and item3) and want to "trigger" something when an item is selected, how do I accomplish this? I know how to create a super simple switch/case statement using ConsoleReadLine, but how do I "read from the listbox"?
lee_sl
Contributor
2990 points
584 Posts
09-02-2008 8:36 AM |
look at SelectionChanged eventhandler which gets fired when the selection changes
sladapter
All-Star
17439 points
3,172 Posts
09-02-2008 9:21 AM |
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { // If you used DataBinding by setting ListBox.ItemsSource, now you can get the selected data
YourDataObject data = listbox.SelectedItem as YourDataObejct;
OR:
// If you build the ListBox by add ListBoxItem :
ListBoxItem item = listbox.SelectedItem as ListBoxItem;
}
09-03-2008 2:10 AM |
Thanks :) I seem to get the variable ok, but how can I make a if/else or switch/case statement for the ListBox? If I have a ListBox that has items "one" , "two", "three" and "four", how do I make a if/else or switch/case using the item variable (that is a local variable "item", right?):
I tried something that seemed logical for me ;) but surprise surprise I got errors... I got the impression that the problem is that the ListBoxItems are strings. How can I refer to a ListBoxItem that is a string? Do I have to convert it to another type or what?
Silverli...
Participant
1133 points
193 Posts
09-03-2008 2:23 AM |
Hi
ListBox.SelectedItem returns and object of type set as item. Yuo cannot cast it to ListBoxItem.
For example how to use ListBox check here: http://www.silverlightshow.net/items/Animating-ListBox-items-the-VisualStateManager.aspx
09-03-2008 2:27 AM |
check also this thread: http://silverlight.net/forums/t/23672.aspx
One of the answers is about SelectedItem:
"Hi
ListBox _listbox = (ListBox)sender;ListBoxItem _item = (ListBoxItem)_listbox.SelectedItem;
in the code above, you will see an InvalidCastException in the second row - at least I have such. This is because the listbox.SelectedItem returns an object of the type the ListBox.ItemSource collection items type. So If your collection contains objects of type CONTENT, SelectedItem will return object of type CONTENT and the cast will fail."
09-03-2008 3:00 AM |
Ok, thanks for the example. Unfortunately I don't understand it :( I don't have a separate cs-file to "populate" the listsbox, I just created it with Blend. Are you saying that there's no way to easily get the selected item (I mean the content of the item) and do something with it? The solution from Sladapter looks simple enough even for me, but as I said there are problems.
Could someone explain to me what can I do with the variable like this, or is it totally useless?
I understood that now I have a local variable that is the selected item. But as a newbie I don't really know what that means in practice. Does the local variable contain the content or what?
09-03-2008 3:13 AM |
Hi,
It is possible:
item.Content will return the content of the ListBoxItem ;-)
In my test project I have the following xaml:
<ListBox x:Name="lbTest" DisplayMemberPath="Name" SelectionChanged="lbTest_SelectionChanged"> <ListBoxItem Content="Item One"/> <ListBoxItem Content="Item Two"/> </ListBox>
after this the value contains what you need. If you know the type you can use it and cast to it.
09-03-2008 5:55 AM |
Thanks again :) Still unsure about the casting and other terminology. So I need to convert the variable.Content to a string? Found some info on that and scribbled something miserable like this:
string choice = item.Content.ToString();
The syntax is correct (or not so far away from correct), because it builds up fine, but of course nothing works :) I guess you understand from the code above, how green I am as a C# coder ;)
09-03-2008 6:43 AM |
item.Content gives you the object set as ListBoxItem content. In my example the content was string value. But if you want the Content can return some business object or any other object.
So if your ListBoxItem content has string values, then "string choice = item.Content.ToString();" is ok.
Btw, I sugest you to not use ListBoxItem, but making a List<> and set it as ItemsSource of the ListBox.
09-03-2008 7:10 AM |
Ok, so I actually understood at least a bit :) It seems that my code does nothing, because Visual Studio 2008 wont show me anything related to those variables (code below) while debugging no matter what break point is used. With some other simple variable related syntaxes the breakpoint do actually give some info.This can be just me and my short experience with Visual Studio, but anyway...
public void valikko_SelectionChanged(object sender, SelectionChangedEventArgs e){ ListBoxItem item = ((ListBox)sender).SelectedItem as ListBoxItem;string valinta = item.Content.ToString();loota2.Text = valinta;}
I suppose that code above *should* get the content of the selected ListBoxItem of ListBox called "valikko" and eventually display it in a text box called "loota2", right? Sorry, I'm a troubling you constantly, but I have to get somekind of an understanding :) You've been very kind already.
09-03-2008 8:15 AM |
It should work ONLY if you add in the xaml the items:
<ListBox x:Name="lbTest" DisplayMemberPath="Name" SelectionChanged="lbTest_SelectionChanged"><ListBoxItem Content="Item One"/><ListBoxItem Content="Item Two"/></ListBox>
But if you intend to use ListBox.ItemsSource proprty to specify list of items it will not work.
My suggestion is to do the following:
Xaml
<ListBox x:Name="lbTest" DisplayMemberPath="Name" SelectionChanged="lbTest_SelectionChanged"></ListBox>
In the code C#:
Somewhere in the code /in the constructor, for example/ you initialize the ListBox with the items you want:
List itemsToShow = new List<string>();itemsToShow.Add( "Item One" );itemsToShow.Add( "Item Two" );lbTest.ItemsSource = itemsToShow;
The SelectionChanged Event:
public void valikko_SelectionChanged(object sender, SelectionChangedEventArgs e){ loota2.Text = ((ListBox)sender).SelectedItem;}
Because the SelectedItem will return the selected object from the underlying ItemsSource list.
If the contains objects from another type, for example Person, the SelectedItem will return the selected Person object, not ListBoxItem.
This is the difference when you use ItemsSource and when using ListBoxItems.
09-04-2008 1:01 AM |
Thank you so much, it works now :) So, eventually I had everything else in place except this in the xaml "SelectionChanged="lbTest_SelectionChanged" I sure remember that thing now ;)
09-04-2008 2:35 AM |
HI,
noce to hear you've made it.
Just to menation - you can attach the event from the code also ;-)
If this thread is answered, please, mark it as answered. 10x.