Question

I am having this problem with my listbox, notifypropertychange event, and setting the selectedindex = -1 upon the transition to the next page.

Basically, I select an item from a list. In doing so it opens up to another page to give details about the item. When I select the item, however, I need to make the selectedindex set to -1 after it transitions to the details page.

Yet, when I write the code, once it hits the selectedindex = -1; line, it calls the function again due to the propertychange event firing. Then it passes a null value to the details page, where the application breaks.

Here is my relevant code:

list of feeds to select from. Once you select a particular one, Listbox_SelectionChanged fires.

  public UserSubmitted()
    {
        InitializeComponent();
        Loaded += delegate(object sender, RoutedEventArgs e)
                  {
                      this.DataContext = App.ViewModel;
                  };
    }

    private void listbox_SelectionChanged(object sender, SelectionChangedEventArgs e)
       {
               NavigationService.Navigate(new Uri("/PivotPage1.xaml", UriKind.Relative));
               hello.SelectedIndex = -1;   //hello = listbox name.
       }

This is what is called next, which is my ViewData Model. It begins at the PropertyChanged event, and then compares the sender. It then proceeds to set the SelectedItem.

  public ObservableCollection<RssItem> FeedItems
    {
        get { return _feedItems; }

        set
        {
            if (_feedItems == value)
                return;
            _feedItems = value;
            NotifyPropertyChanged(this, new PropertyChangedEventArgs("FeedItems"));
        }
    }

    public string FeedTitle
    {
        get { return _FeedTitle; }
        set
        {
            if (_FeedTitle == value)
                return;
            _FeedTitle = value;
            NotifyPropertyChanged(this, new PropertyChangedEventArgs("FeedTitle"));
        }
    }

    public RssItem SelectedItem
    {
        get { return _SelectedItem; }
        set
        {
            if (_SelectedItem == value)
                return;
            _SelectedItem = value;
            NotifyPropertyChanged(this, new PropertyChangedEventArgs("SelectedItem"));
        }
    }


    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(object sender, PropertyChangedEventArgs args)
    {
        if (PropertyChanged != null)
            PropertyChanged(sender, args);
    }

It then proceeds to go to the line: hello.SelectedIndex = 1; where it goes through the above code again, but passing everything as null (as it's all set to -1).

Then it proceeds to pass this information to the pivot page (the details page).

    public PivotPage1()
    {
        InitializeComponent();
        Loaded += (sender, e) =>
            {
                this.DataContext = App.ViewModel;
                var selectedItem = App.ViewModel.SelectedItem;
                var pi = ItemPivot.Items.First(p => p == selectedItem);  //breaks here as null.
                ItemPivot.SelectedItem = pi;
            };
    }

anyone know why this is?

Was it helpful?

Solution

I'd check in the listbox_SelectionChanged method whether the selected index is already -1. If it is, then just return and do nothing.

private void listbox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (hello.SelectedIndex == -1) return;

    NavigationService.Navigate(new Uri("/PivotPage1.xaml", UriKind.Relative));
    hello.SelectedIndex = -1;   //hello = listbox name.
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top