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

ObservableCollection not firing proper CollectionChanged event

$
0
0

WPF and EntityFramework 6
I've created entity Customer, then in CustomerViewModel I've implemented ObservableCollection and subscribed to event CollectionChanged.
In mainWindow DataGrid.ItemsSource is assigned to Customers ObservableCollection. As far as I know, it is reference type.

Then, for testing purposes I'm adding new Customer and adding it to customerView.Customers collection.

Why it fires collectionchanged event in mainWindow? I'm changing collection in viewModel. Seems like connection between Datagrid and viewmodel is broken. (But, after adding item, it is saved to database and presented in UI)

what I want to achieve is adding items to database when entering new row in Datagrid manually. Any ideas how to fix it?

Edit:Maybe my entity Customer should implement INotifyPropertyChanged instead of ViewModel?

   public class CustomerViewModel : ObservableObject
    {
        private Customer Customer = new Customer();
        private Context ctx;
        public Context Context
        {
            get
            {
                return ctx;
            }

        }
     
        public CustomerViewModel()
        {
            ctx = new Context();
            Customers = new ObservableCollection<Customer>();
            Customers.CollectionChanged += OnCollectionChanged;
        }

        private void OnCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            MessageBox.Show("Test");
        }

        public ObservableCollection<Customer> Customers
        {
            get
            {
                return Customer.Customers;
            }
            set
            {
                if (Customer.Customers != value)
                {
                    Customer.Customers = value;
                    RaisePropertyChanged("Customers");
                }
            }
        }

        public int CustomerNumber
        {
            get
            {
                return Customer.CustomerNumber;
            }

            set
            {
                if (Customer.CustomerNumber != value)
                {
                    Customer.CustomerNumber = value;
                    RaisePropertyChanged("CustomerNumber");
                }

            }

        }

        public ObservableCollection<Customer> getAllCustomers()
        {
                ctx.Customers.Load();
                Customer.Customers = ctx.Customers.Local;
                return Customer.Customers; 
        }
	}

		CustomerViewModel viewModel = new CustomerViewModel();
        public MainWindow()
        {
            InitializeComponent();       
        }

        private void Main_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                DataGrid1.ItemsSource = viewModel.getAllCustomers();
                ((INotifyCollectionChanged)DataGrid1.Items).CollectionChanged += OnCollectionChanged;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.InnerException.ToString());
            }            
        }

        private void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            MessageBox.Show("Test2");
        }
		private void RibbonButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                viewModel.Customers.Add(new Customer { CustomerNumber = 214 });            
                viewModel.Context.SaveChanges();              
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }            
        }



Viewing all articles
Browse latest Browse all 18858

Trending Articles



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