Skip to main content
Home Forums Silverlight Programming Silverlight Controls and Silverlight Toolkit Un-select item in listbox
6 replies. Latest Post by sladapter on July 17, 2008.
(0)
chadhe
Member
1 points
3 Posts
07-16-2008 4:41 PM |
How do you un-select all items in a listbox?
I thought lb.SelectedIndex = -1; would do it, but nothing happens (visually).
Thanks!
sladapter
All-Star
17439 points
3,172 Posts
07-16-2008 5:08 PM |
lb.SelectedItem = null;
07-16-2008 5:56 PM |
Thanks for your response. I realize now that my whole problem is that I am trying to un-select inside the SelectoinChanged event. Setting the SelectedItem to null seems to have caused the list box to act like it’s in mult-select mode. Meaning that the currently selected item remains selected even after I select another row. Both visually it looks selected as well as doesn’t fire the SelectionChanged event. I have verified that both setting SelectedIndex = -1 and SelectedItem = null will un-select from other user triggered events (like clicking a button).I'm guessing I'm just out of luck for this case. Are there any good techniques for queuing up an action to happen later?
tgrand
Participant
788 points
173 Posts
07-16-2008 6:00 PM |
chadhe:Are there any good techniques for queuing up an action to happen later?
Check out Dispatcher.BeginInvoke. Not sure if it's what you want in this situation, but it's worth a look anyway.
07-16-2008 8:56 PM |
I tried, Dispatcher.BeginInvoke does not work.
Use a System.Windows.Threading.DispatcherTimer would work:
System.Windows.Threading.DispatcherTimer timer = new System.Windows.Threading.DispatcherTimer(); public Page() { InitializeComponent(); timer.Interval = TimeSpan.FromMilliseconds(50); timer.Tick += new EventHandler(timer_Tick); }
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { timer.Start(); }
void timer_Tick(object sender, EventArgs e) { theListBox.SelectedItem = null; timer.Stop(); }
07-17-2008 11:23 AM |
This works great. Thanks for the suggestion. It definitely feels a lot like the old setTimeout workaround we had to do in JavaScript for web development in the past, but I think this can’t have race conditions like setTimeout did because the event is dispatched to the UI thread, meaning the thread has to be idle. Still seems like a hack, but it should be a reliable and safe workaround to my problem. Thanks again.
07-17-2008 11:29 AM |
Yes, I got the same idea because I had to use setTimeout in JavaScript as a workaround for Mac a lot. I found it I have to do this in Silverlight in several cases already.