Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi!

I´m facing a problem with Silverlight.

Situation:
1. A listbox
2. A webservice that gets data from a SQL database

I can fill the listbox with the result from the webservice. Nothing fancy there.
Just the listbox itemssource gets filled.

But when I add a timer to the XAML that sets the itemssource in every 10 seconds, I lose the selection.
So, timer runs: Itemssource is set with the result of the webservice. I select an item in the listbox. 10 seconds later the timer sets the itemssource again.
The selected item is unselected.

I have search on Google and in CP. I cannot find a good solution for this.

Anyone a good idea/example/link?

Thanks
Posted
Updated 19-May-11 13:20pm
v2
Comments
Dalek Dave 19-May-11 19:21pm    
Good question.

So save the SelectedIndex and SelectedItem properties BEFORE calling the web service, and in the async completed event hander, restore the selection.

*added* Just use the SelectedItem, as the index may change with the new data set.

*added by edman196* : As OP pointed out, SelectedItem is unreliable. I would suggest storing the a copy of the selected object or string and then reselcting by doing the following:

C#
object SelectedObject = TheListBox.Items[TheListBox.SelectedIndex];
//Update TheListBox items
TheListBox.SelectedIndex = TheListBox.Items.IndexOf(SelectedObject);
//Or an equivalent line to above if that doesn't work exactly :) 
 
Share this answer
 
v3
Comments
Ed Nutting 19-May-11 16:23pm    
Good answer, my 5 :) Though do see my improvement :)
Uhm... I remember trying that already, but because of the itemtemplate in the listbox the selecteditem was giving some problems. Maybe I made a mistake then and will try it tomorrow again.

Thanks
 
Share this answer
 
Comments
Mark Salsbery 19-May-11 17:01pm    
SelectedItem should be an object reference to an item in your data collection...how would it know anything about itemtemplates? :) Of course, if all the items in the collection change each time it's refreshed (as opposed to adding new items to the end of the existing collection), then the SelectedItem will always be different so you'd have to use the index.
Ken Elz 20-May-11 3:19am    
But the selectedindex can change to, as said before. I'll try the selecteditem first. If that does not work I'll have to find something different. My problem isn't that hard, right? :)

Ps. I love the itemtemplates. Makes designing a lot easier :)
Mark Salsbery 21-May-11 22:16pm    
If the selecteditem (which is an object reference) and the selectedindex can change between reloading the itemssource, then you have no alternative but to come up with your own scheme to track the previous selected item and find that item's index on the next load.
Alright, I have tried both:

1. Store selecteditem
Gives an error.

private ListBoxItem SelectedObject;

... rest of code ...

C#
void Update_Completed(object sender, UserService.GetUsersCompletedEventArgs e)
{
listBox1.ItemsSource = e.Result;

listBox1.SelectedIndex = SelectedObject; //Error on selectedobject
}


2. Selectedindex

private object SelectedObject;

... rest of code ...

Before calling the webservice:
SelectedObject = listBox1.SelectedItem;

C#
void Update_Completed(object sender, UserService.GetUsersCompletedEventArgs e)
{
listBox1.ItemsSource = e.Result;
listBox1.SelectedIndex = listBox1.Items.IndexOf(SelectedObject);
}


Previous selected item in the list box is not selected after reloading the listbox.

Any other ideas?
 
Share this answer
 

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