locked
Many windows appears of click on Button ADD -MVVM WPF RRS feed

  • Question

  • Hi all,

    I have Button ADD. When I click on it, I have a new window. I add the new Customer, my datagrid refreshes but just the first time! Then every time I click on Button Add, I have a new window with the same information and so on... I get many windows ADD!

    ViewModel:

     private ObservableCollection<Custmor> _loadDataBinding;    
        public ObservableCollection<Custmor> loadDataBinding
        {
            get
            {
                return _loadDataBinding;
            }
            set
            {
                _loadDataBinding = value;
                OnPropertyChanged("loadDataBinding");
            }
        }
    
    
         public ViewModel1()
        {
                 //Display my DataGrid 
            using (Test1Entities context = new Test1Entities())
            {
                _loadDataBinding = new ObservableCollection<Custmor>(context.Custmor.ToList());
            }
    
            //To Show the Window ADD from Window MainWindow
            addCustomerMainWindow = new RelayCommand(add, canexecute);          
    
    
            CustomerToAddObject = new Custmor();
    
    
            addCustomer1 = new RelayCommand(ADDFunction);       
        }   
    
    
    
        //To Show the Window ADD from Window MainWindow
        private ICommand addCustomerMainWindow;
        public ICommand AddCustomerMainWindow
        {
            get { return addCustomerMainWindow; }
        }
    
        private void add(object obj)
        {          
            Add addView = new Add();           
            addView.DataContext = this;
            addView.Show();      
        }
    
        //The Button ADD from Window ADD
        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!");                
            }         
    
            _loadDataBinding.Add(CustomerToAddObject);
        }       
    
    
    
       //textBox for window ADD: CustomerToAddObject.nom ;CustomerToAddObject.prenom and CustomerToAddObject.reference
      private Custmor customerToAddObject;
    
        public Custmor CustomerToAddObject
        {
            get { return customerToAddObject; }
            set { customerToAddObject = value; }
        }

    How can I fix it for when I ADD a customer, the window ADD closes, and the Mainwindow still updates and so on? Clicking on the button ADD and it adds new customers, as many times as I want?

    The result of my error: 

    Thanks for your help,

    Monday, May 22, 2017 4:35 PM

Answers

  • I found the solution for the problem: open many windows ADD :

    i modify the functions add and ADDFunction

    the code in ViewModel:

    private Add addView;
    private void add(object obj)
    
        if (addView != null)
            addView.Close();
    
        addView = new Add();
        addView.DataContext = this;
        addView.Show();
    }
    
    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);
    
        if (addView != null)
            addView.Close();
        CustomerToAddObject = new Custmor();
    }

    • Marked as answer by Thabetidris Tuesday, May 23, 2017 11:32 AM
    Tuesday, May 23, 2017 11:32 AM

All replies

  • Hi,

    To close the window, in the "Add" button of the Add window you need to do something like this:

    this.Close();



    Please Mark as Answered if this answers your question Or UnMark as Answered if it did not. Happy to Help :)

    Tuesday, May 23, 2017 5:33 AM
  • Hi,

    According to your description, I suggest you can upload your demo which can reproduce your problem to OneDrive(Including your test material). We can download it and debugging. This will help us quickly analyze your problem.

    Share OneDrive files and folders:
    https://support.office.com/en-us/article/Share-OneDrive-files-and-folders-9fcc2f7d-de0c-4cec-93b0-a82024800c07

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 23, 2017 6:30 AM
  • thanks for this idea, i will upload my test application 
    Tuesday, May 23, 2017 11:28 AM
  • thanks,
    but the problem is this.close() doesn't exist! I don't know what si the problem
    Tuesday, May 23, 2017 11:30 AM
  • I found the solution for the problem: open many windows ADD :

    i modify the functions add and ADDFunction

    the code in ViewModel:

    private Add addView;
    private void add(object obj)
    
        if (addView != null)
            addView.Close();
    
        addView = new Add();
        addView.DataContext = this;
        addView.Show();
    }
    
    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);
    
        if (addView != null)
            addView.Close();
        CustomerToAddObject = new Custmor();
    }

    • Marked as answer by Thabetidris Tuesday, May 23, 2017 11:32 AM
    Tuesday, May 23, 2017 11:32 AM