简体   繁体   中英

ListPicker SelectionChanged gets called multiple times

the ListPicker is a Control from the WP8 Toolkit.

Code:

private void field_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    // Make sure we don't handle the event during initiation.
    if (e.RemovedItems != null && e.RemovedItems.Count > 0)
    {
        if (this.field.SelectedItems != null)
        {
            if (this.field.SelectedIndex != -1)
            {
                ListPicker_SelectionChanged(sender, e);
                //Make needed proffesions visable:
                profls.Clear();
                foreach (ListPickItem item in field.SelectedItems)
                    switch (item.Tag)
                    {
                        default:
                            foreach (ListPickItem iitem in profl[9])
                                profls.Add(iitem);
                            break;
                        case 90017:
                            foreach (ListPickItem iitem in profl[0])
                                profls.Add(iitem);
                            break;
                        case 9000:
                            foreach (ListPickItem iitem in profl[1])
                                profls.Add(iitem);
                            break;
                    }
            }
        }
    }
}
  • Please notice that a profession ListPicker's ItemsSource is Data Binded to the profls var.
  • I modified the Listpicker so that I can also set the SelectedItems property and not only read from it (following this guide) and it works great.

Problem:

The field_SelectionChanged event gets called multiple times whenever I change the field listpicker's selecteditems. (i want it to be called only once..) Another wierd thing is that on one of the last calls the field_SelectedItems is equal to the old selectedItems (the ones before the "change")..

Is it a bug or my problem? (How do I fix it?)


EDIT:

I checked and it appears that it gets called only once if the are no selected items in the listpicker before I select items. (I mean that SelectedItems is empty before I select new items)

Fixed :)

I used the code suggested in this answer: listPicker not updating selection in full mode

if (MyListPicker.SelectedIndex != -1)
{
 //Code..
}

I had the same issue of the selectedchange event being called twice.At the end of the listPicker_selectedchange event, set the listpicker selected index to -1.

private void listpicker_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
  //Code
  listpicker.SelectedIndex = -1;
}

Thank you Dan Barzilay!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM