Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi,

I have MainWindow and Window Add , ViewModel and ViewModelADD..l'addition of new Customer is correct in database, but the refresh of DataGrid no ! and when i finish l'addition , the Window ADD doesn't close !

ViewModel:

ViewModelADD:

How can I fix it?
to add new Customer..and refresh the DataGrid, and when the Customer added..the Window ADD close

Thanks,

What I have tried:

C#
<pre>private static ViewModel1 instance = new ViewModel1();
        public static ViewModel1 Instance { get { return instance; } }
		
		
		    private void add(object obj)
        {
			Add addView = new Add();
            addView.DataContext = new ViewModelADD(loadDataBinding);
            addView.Show();
		}
		
		 private ObservableCollection<Custmor> _loadDataBinding;     

        public ObservableCollection<Custmor> loadDataBinding
        {
            get
            {
                return _loadDataBinding;
            }

            set
            {
                _loadDataBinding = value;
                OnPropertyChanged("loadDataBinding");
            }
        }

C#
<pre> public ViewModelADD(ObservableCollection<Custmor> loadDataBinding)
        {
            CustomerToAddObject = new Custmor();

            addCustomer1 = new RelayCommand(ADDFunction);
        }
		
		
		 private ICommand addCustomer1;      
          public ICommand AddCustomer1
        {
            get { return addCustomer1; }
        }

       
        private void ADDFunction(object obj)
        {
            using (Test1Entities context = new Test1Entities())
            {
                context.Custmor.Add(customerToAddObject);
                context.SaveChanges();
                MessageBox.Show("Customer a été ajouté avec succès!");
            }

            ViewModel1.Instance.loadDataBinding.Add(customerToAddObject);
Posted
Updated 24-May-17 1:22am

1 solution

I found the solution,
first I modify the ViewModel:

C#
private void add(object obj)
        {        
            Add addView = new Add();
            addView.DataContext = new ViewModelADD(addView,loadDataBinding);
            addView.Show();
        }

then i modify the ViewModelADD:


C#
<pre> private readonly Window _window;       
        private readonly ObservableCollection<Custmor> _loadDataBinding;
		
		 public ViewModelADD(Window window,ObservableCollection<Custmor> loadDataBinding)
        {
            CustomerToAddObject = new Custmor();

            addCustomer1 = new RelayCommand(ADDFunction);
            _window = window;
            _loadDataBinding = loadDataBinding;
        }
		
		
		   private void ADDFunction(object obj)
        {
            using (Test1Entities context = new Test1Entities())
            {
                context.Custmor.Add(customerToAddObject);
                context.SaveChanges();
                MessageBox.Show("Customer a été ajouté avec succès!");
            }
                                
            _loadDataBinding.Add(customerToAddObject);
            _window.Close();           
        }



It works correct now,
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900