locked
SQLite and Observablecollection

    Question

  • Hello,

    somehow changing my get-statement to querying a database loses the observablecollection function. Why?

    private ObservableCollection<myCats> cats = new ObservableCollection<myCats>();
    
    public ObservableCollection<myCats> Cats
            {
                get { return cats;
                }
                set { // };
    
    

    This works getting data back to my xaml when I update it.

    This does not. Why? Is it because I have a new-statement inside the function? If so how do I fix it?

    get { using (var db = new SQLiteConnection(databasePath)) { cats = new ObservableCollection<myCats> (from info in db.Table<myCats>() select info); return cats; }

    }


    Saturday, June 7, 2014 8:52 PM

Answers

  • The Xaml is still bound to the original ObservableCollection. It doesn't know that you've created a new one.

    When you update a property you need to report that it's changed by implementing INotifyPropertyChanged. The ObservableCollection will do this for its contents, but you'll need to do it for the Cats property.

    You may be better off copying the table into the existing OC rather than creating a new one.
    Saturday, June 7, 2014 9:07 PM
    Owner

All replies

  • The Xaml is still bound to the original ObservableCollection. It doesn't know that you've created a new one.

    When you update a property you need to report that it's changed by implementing INotifyPropertyChanged. The ObservableCollection will do this for its contents, but you'll need to do it for the Cats property.

    You may be better off copying the table into the existing OC rather than creating a new one.
    Saturday, June 7, 2014 9:07 PM
    Owner
  • You don't need to Get, Set the Observation collection just implement the INotifyPropertyChanged in myCats. Then you can bind it to a ListView.

    cats.Add(new ObservableCollection<myCats>(from info in db.Table<myCats>() select info));

    //or

    cats.Add(new ObservableCollection<myCats>(){Name = from info in db.Table<myCats>() select info});

    MyListView.ItemsSource = myCats;

     

    ObservableCollection<myCats> cats = new ObservableCollection<myCats>();

    public class myCats : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; public void NotifyPropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } private string name; public string Name { get { return name; } set { name = value; NotifyPropertyChanged("Name"); } } }




    • Edited by St4mos Sunday, June 8, 2014 10:35 PM
    Sunday, June 8, 2014 8:25 PM
  • You don't need to Get, Set the Observation collection just implement the INotifyPropertyChanged in myCats. Then you can bind it to a ListView.

    cats.Add(new ObservableCollection<myCats>(from info in db.Table<myCats>() select info));

    //or

    cats.Add(new ObservableCollection<myCats>(){Name = from info in db.Table<myCats>() select info});

    MyListView.ItemsSource = myCats;

     

    ObservableCollection<myCats> cats = new ObservableCollection<myCats>();

    public class myCats : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; public void NotifyPropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } private string name; public string Name { get { return name; } set { name = value; NotifyPropertyChanged("Name"); } } }





    Hi St4mos!

    My "Model"-class doesn't implement INotifypropertychanged. Would you recommend that? I thought that as a part of the Model of MVVM it wasn't a requirement?

    Becuase in my viewmodel, in my Set-statement, I have the OnNotifyPropertyChanged.

    Monday, June 9, 2014 8:45 AM
  • Hi Rob,

    could you explain how mean to copy the table?

    As mentioned my xaml is bound to the Cats-collection.

    And my update statement is bound to a NewCat-propety such as.

    Cats.add(NewCat)

    Tuesday, June 10, 2014 10:42 AM
  • It's not completely clear what you're doing, but it sounds like you're bound to the old ObservableCollection. When you replace it you're still bound to the old one, so you either need to update the binding or keep using the old one but clear out its contents and add in the newly queried contents.

    Thursday, June 12, 2014 3:55 AM
    Owner