Frage

I'm having trouble in trying to swap/move items in an ObservableCollection. I have a list of items in a listbox, when you swipe an item, it becomes a completed item and hence moves to the end of the list.. the subsequent times, shifts up.. eg: if there are 3 items. when we swipe on the first item, the first item moves to the bottom of the list making it the third item, the second item moves up becomes first, and the third item moves up to become the 2nd item.

This is how i have declared the instance

private ObservableCollection<PageCollection> PageCollectionObservableCollection { get; set; } 

and inside the constructor of the class, it is defined like this where PageCollection is the class to which the listbox is databound.

PageCollectionObservableCollection = new ObservableCollection<PageCollection>();

And while I try to swipe on an item in a Listbox which is data bound, the following code is executed.

       PageCollection completedItem = fe.DataContext as PageCollection;
       int fileNoCompleted = completedItem.FileNo;
       if (completedItem.CompletionStatus == false)
       {
            int position = 0;

            for (int i = PageCollectionObservableCollection.Count - 1; i > -1; i--)
            {
                if (PageCollectionObservableCollection.ElementAt(i).CompletionStatus == false)
                {
                    position = i;
                    break;
                }
            }
            completedItem.CompletionStatus = true;
            completedItem.Color = Colors.Green;
            completionStatusArray[fileNoCompleted] = true;
            PageCollectionObservableCollection.Move(fileNoCompleted, position); // ERROR
       }  

When execution reaches PageCollectionObservableCollection.Move the following error is thrown.

An exception of type 'System.NotSupportedException' occurred in System.Windows.ni.dll but was not handled in user code

The above code worked like a charm in Windows 8, the but now it throws an exception in WP8. any idea?

War es hilfreich?

Lösung

The control is bound to a Listbox. I found a work around. I'm not sure if its the right method. I created a temporary OC, did the move function and then reassigned it back to the old OC. Then updated the ListBox.

   PageCollection completedItem = fe.DataContext as PageCollection;
   int fileNoCompleted = completedItem.FileNo;
   if (completedItem.CompletionStatus == false)
   {
        int position = 0;

        for (int i = PageCollectionObservableCollection.Count - 1; i > -1; i--)
        {
            if (PageCollectionObservableCollection.ElementAt(i).CompletionStatus == false)
            {
                position = i;
                break;
            }
        }
        completedItem.CompletionStatus = true;
        completedItem.Color = Colors.Green;
        completionStatusArray[fileNoCompleted] = true;

//EDIT

    List<PageCollection> l = PageCollectionObservableCollection.ToList<PageCollection>();
        ObservableCollection<PageCollection> tempPageCol = new ObservableCollection<PageCollection>(l);
        tempPageCol.Move(fileNoCompleted, position);

        PageCollectionObservableCollection = tempPageCol;

        CategoryLB.ItemsSource = PageCollectionObservableCollection;
        CategoryLB.UpdateLayout();


   }  
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top