Skip to main content

Microsoft Silverlight

Answered Question Un-select item in listboxRSS Feed

(0)

chadhe
chadhe

Member

Member

1 points

3 Posts

Un-select item in listbox

How do you un-select all items in a listbox?

 I thought lb.SelectedIndex = -1; would do it, but nothing happens (visually).

Thanks!

sladapter
sladapter

All-Star

All-Star

17439 points

3,172 Posts

Re: Un-select item in listbox

 lb.SelectedItem = null;

 

sladapter
Software Engineer
Aprimo, Inc

Please remember to mark the replies as answers if they answered your question

chadhe
chadhe

Member

Member

1 points

3 Posts

Re: Un-select item in listbox

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
tgrand

Participant

Participant

788 points

173 Posts

Re: Un-select item in listbox

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.

sladapter
sladapter

All-Star

All-Star

17439 points

3,172 Posts

Answered Question

Re: Un-select item in listbox

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();
        }

sladapter
Software Engineer
Aprimo, Inc

Please remember to mark the replies as answers if they answered your question

chadhe
chadhe

Member

Member

1 points

3 Posts

Re: Un-select item in listbox

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.  

sladapter
sladapter

All-Star

All-Star

17439 points

3,172 Posts

Re: Un-select item in listbox

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.

 

 

sladapter
Software Engineer
Aprimo, Inc

Please remember to mark the replies as answers if they answered your question

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities