Search code examples
windows-phone-7windows-phonewindows-phone-8

Windows Phone 7: How to notify data is updated in ListBox?


enter image description here

I have above scenario: If user click on ListBox, it will either have sub items (again ListBox) or detail view.

So what i did currently is: Whenever user clicks any item, made a web call and filled up the same ListBox if clicked item is having further sub items.

Now, issue comes in picture:

  1. Suppose i am in 4th screen (detail view),
  2. Moved to the 3rd and 2nd screen with data maintained as stack (Its working fine, yes i am maintaining data in ObservableCollection<ObservableCollection<MyObjects>> so while moving back, i am fetching data from there)
  3. Now, if i click any item in screen 2, it will open detail view for the screen 3 ListBox data.

Means that ListBox is not getting notified that we have filled data inside OnBackKeyPress()

FYI, i am filling up ListBox and WebBrowser in the same page., so my problem is that how do i notify ListBox once i filled up data from stack which i have maintained?

Yes i have also implemented INotifyPropertyChanged but don't know why its not working.

Please check my code:

  1. ListBox and WebView screen: http://pastebin.com/K1G27Yji
  2. RootPageItem class file with the implementation of INotifyPropertyChanged: http://pastebin.com/E0uqLtVG

sorry for pasting code in above way, i did as question is being long.

Problem:

How do i notify ListBox that data is changed from OnBackKeyPress?


Solution

  • Oops it was a silly mistake i made.

    I forgot to set items[] array inside OnBackKeyPress() but was accessing while clicking item, hence its having items[] data of last step we moved in forward direction, it was executing the same data.

    Now, i have just included a single line and it has solved my problem.

     items = listRootPageItems.ToArray();  // resolution point
    

    So final code of onBackKeyPress() is:

    /**
     * While moving back, taking data from stack and displayed inside the same ListBox
     * */
     protected override void OnBackKeyPress(CancelEventArgs e)
     {
         listBox1.Visibility = Visibility.Visible;
         webBrowser1.Visibility = Visibility.Collapsed;
         listBox1.SelectedIndex = -1;
    
         if (dataStack.Count != 0)
         {
              listBox1.ItemsSource = null;
              listRootPageItems = dataStack[dataStack.Count-1];
              listBox1.ItemsSource = listRootPageItems;
    
                   items = listRootPageItems.ToArray();  // resolution point
              dataStack.Remove(listRootPageItems);
    
              e.Cancel = true;
          }
     }