Let's say that I have this class as a Model (database generated from it):
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public string City { get; set; }
}
In ViewModel, I have an ObservableCollection Customers that holds all the customers from the database:
ObservableCollection<Customer> Customers;
"Customers" are populated from the database.
In a View, ListBox is populated from Customers:
ListBoxExample.ItemsSource = Customers;
Here is the problem. Let's say I want to insert a new customer into a database, what approach should I take in order to track the changes via UI:
Resetting the ItemsSource: (Currently using this - worst approach)
ListBoxExample.ItemsSource = null;
ListBoxExample.ItemsSource = Customers;
Tracking changes via ObservableCollection Customers
For example, if I want to insert something into database, insert it into Customers too and UI will be notified about the change.
What are the other options?
What is the best MVVM approach to achieve the following:
- Parse JSON data and insert it into local database
- Retrieve data from the local database and populate ListBox/ListView with it
- If new data is inserted into database or item is deleted/changed, update changes in ListBox/ListView
Thank you in advance.
Aucun commentaire:
Enregistrer un commentaire