更新列表框而不丢失选择 [英] Update listbox without losing selection
问题描述
嗨!
我遇到了Silverlight的问题.
状况:
1.列表框
2.一个从SQL数据库获取数据的Web服务
我可以用Web服务的结果填充列表框.没什么可看的.
只是列表框itemssource被填充.
但是,当我向XAML添加计时器以每隔10秒设置itemssource时,我会丢失选择.
因此,计时器运行:使用Web服务的结果设置Itemssource.我在列表框中选择一个项目. 10秒后,计时器将再次设置itemssource.
所选项目未选中.
我可以在Google和CP中进行搜索.我找不到一个好的解决方案.
任何人都有好主意/示例/链接吗?
感谢
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
推荐答案
因此,在调用Web服务之前,请保存SelectedIndex和SelectedItem属性,并在异步完成的事件处理程序中恢复选择.
*添加*只需使用SelectedItem,因为索引可能会随新数据集而变化.
*由edman196添加*:如OP所指出,SelectedItem不可靠.我建议存储选定对象或字符串的副本,然后通过执行以下操作重新选择:
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:
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 :)
嗯...我记得已经尝试过了,但是由于列表框中的itemtemplate,selecteditem出现了一些问题.也许那时我犯了一个错误,明天再试一次.
谢谢
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
好的,我都尝试过:
1.存放selecteditem
出现错误.
私有ListBoxItem SelectedObject;
...其余的代码...
Alright, I have tried both:
1. Store selecteditem
Gives an error.
private ListBoxItem SelectedObject;
... rest of code ...
void Update_Completed(object sender, UserService.GetUsersCompletedEventArgs e)
{
listBox1.ItemsSource = e.Result;
listBox1.SelectedIndex = SelectedObject; //Error on selectedobject
}
2. Selectedindex
私有对象SelectedObject;
...其余的代码...
调用Web服务之前:
SelectedObject = listBox1.SelectedItem;
2. Selectedindex
private object SelectedObject;
... rest of code ...
Before calling the webservice:
SelectedObject = listBox1.SelectedItem;
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?
这篇关于更新列表框而不丢失选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!