Reputation: 2442
I am new to WPF and have a beginner question. Whenever I added data to a collection my UI was only getting updated after I restarted the program. I was originally using ICollection but realized I need to use OvservableCollection to update the collection. When I swtiched the Customers property from ICollection to ObservableCollection I get an error on my UpDate method saying I can't implicitly convert. Is possible to cast an ObservableCollection. How else could I fix this issue? Thanks in advance.
ViewModel.cs
public ViewModel()
{
Customers = new ObservableCollection<Customer>();
UpDate();
}
public void UpDate()
{
Customers.Clear();
foreach (var customer in context.Customers.OrderBy(c => c.Name))
{
Customers.Add(customer);
}
}
#region Add new customer,project,program,rev methods
public void AddCustomer(string customerName)
{
using (context = new RevisionModelContainer())
{
var customer = context.Customers;
customer.Add(new Customer { Name = customerName });
context.SaveChanges();
UpDate();
}
}
public ObservableCollection<Customer> Customers { get; set; }
public ObservableCollection<Project> Projects { get; set; }
public ObservableCollection<Program> Programs { get; set; }
public ObservableCollection<Revision> Revisions { get; set; }
public DateTime Dates { get; set; }
public string Notes { get; set; }
Customer.cs
public partial class Customer
{
public Customer()
{
this.Projects = new ObservableCollection<Project>();
}
public int Id { get; set; }
public string Name { get; set; }
public virtual ObservableCollection<Project> Projects { get; set; }
}
Upvotes: 0
Views: 102
Reputation: 33364
create instance of Customers
in ViewModel
constructor
public ViewModel()
{
Customers = new ObservableCollection<Customer>();
UpDate();
}
and populate the list when UpDate
is called
public void UpDate()
{
Customers.Clear();
foreach(var customer in context.Customers.OrderBy(c => c.Name)) Customers.Add(customer);
}
Upvotes: 2