简体   繁体   中英

ObservableCollection<T> fires SelectionChanged event if populated in views constructor

I have data saved in the IsolatedStorage in my WP7 app, this data is a ObservableCollection

I then load the data into a observablecollection in the app that is databinded to a listview with a datatemplate

But when I do this (Or just add data to the databound list) in the constructor it fires a ListBox selectionchanged event, so before my app is fully loaded this happens.

I have an event for selectionchanged to show details about the clicked object and this crashes when this happens (Selectedindex is 0 for some reason so object 1 in the loaded list is selected automaticly when loaded)

public partial class MainPage : INotifyPropertyChanged
{
    public ObservableCollection<Note> NotesCollection { get; set; }
    public CollectionViewSource NotesViewSource;
    private readonly IsolatedStorageSettings settings;

    // Constructor
    public MainPage()
    {
        InitializeComponent();
        NotesCollection = new ObservableCollection<Note>();

        settings = IsolatedStorageSettings.ApplicationSettings;

        if (settings.Contains("Notes"))
        {
            NotesCollection = (ObservableCollection<Note>)settings["Notes"];
        }
        else
        {
            settings.Add("Notes", NotesCollection);
        }

        NotesViewSource.View.Refresh();

        //var note = new Note("hej", "hej", DateTime.Now, DateTime.Now);
        //NotesCollection.Add(note); this also fires the event

        NotesViewSource = new CollectionViewSource { Source = NotesCollection };
        DataContext = this;
        ListBoxNotes.ItemsSource = NotesViewSource.View;
    }

my Selectionchanged

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

        var note = ListBoxNotes.SelectedItem as Note;

        if (!(note is Note)) return;

        (Application.Current as App).Note = note;

        ListBoxNotes.SelectedIndex = -1;
        NavigationService.Navigate(new Uri("/Views/DetailsView.xaml", UriKind.Relative));
    }

If you want to add items to the OC before any bindings may fire, then move the following line

InitializeComponent();

after the point where items are added. When this method is called, all the UI is created and bindings are set. You can right-click and go to definition to see it happening.

I would tie into the Loaded event.

Use a private and public. Notice the lowercase for the private.

  private ObservableCollection<Note> notesCollection 

Make SelectedIndex a public property and bind to it. When you assign the private side set it to -1;

  private int selectedIndex = -1;

By default the selected index is 0. And selected index changed is always going to fire when the app starts. You just need to set selectedIndex = -1 before the event is called.

With SelectedIndex as a public property I would do the logic in the set and not even have a changed event.

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