Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Observable Collection not updating WPF UI

$
0
0

I am having a problem updating an ObservableCollection that is binded to a combobox in my WPF UI.  I have an ObservableCollection of Customer objects.  When I try to add a new Customer (AddCustomer method. see code example below) object my UI only updates the combobox if the program is reloaded.  I am implementing INotifyPropertyChanged and calling the PropertyChanged event when I add a customer object.  I'm not sure what I am doing wrong.  Does anyone have a suggestion?  As a note, the Customer class was developed through the entity framework.  I changed all the default ICollection to ObservableCollection. 

public class ViewModel:INotifyPropertyChanged
    {
        RevisionModelContainer context = new RevisionModelContainer();
        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();
                OnPropertyChanged("Customers");
                UpDate();
            }
        }

        private ObservableCollection<Customer> customers;
        public ObservableCollection<Customer> Customers
        {
            get { return customers; }
            set
            {
                customers = value;
                OnPropertyChanged("Customers");
            }
        }
        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; }
  public event PropertyChangedEventHandler PropertyChanged = delegate { };

        private void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            handler(this, new PropertyChangedEventArgs(name));
        }
 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; }
    }




Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>