public void ModifyMethod()
 {
     if (selectedCustomer != null)
     {
         ModifyView change = new ModifyView();
         Messenger.Default.Send(new MessageMember(selectedCustomer, "edit"));
         change.Show();
     }
 }
Beispiel #2
0
        public MainViewModel()
        {
            ButtonIsEnabled = false;
            //Message receivers to accept messages from EditViewModel and AddViewModel
            //The sender also sends a token as the second parameter and only receivers with the same token will accept it.
            Messenger.Default.Register <Customer>(this, "add", (customer) =>
            {
                try
                {
                    // Code a query to retrieve the selected customer
                    // and store the Customer object in the class variable.
                    selectedCustomer = customer;
                    CustomerIDBox    = Convert.ToString(customer.CustomerID);
                    this.DisplayCustomer();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, ex.GetType().ToString());
                }
            });

            Messenger.Default.Register <Customer>(this, "modify", (customer) =>
            {
                try
                {
                    // Code a query to retrieve the selected customer
                    // and store the Customer object in the class variable.
                    selectedCustomer = customer;
                    this.DisplayCustomer();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, ex.GetType().ToString());
                }
            });

            Messenger.Default.Register <Customer>(this, "modData", (customer) =>
            {
                SelectedCustomer.Name    = customer.Name;
                SelectedCustomer.Address = customer.Address;
                SelectedCustomer.City    = customer.City;
                SelectedCustomer.State   = customer.State;
                SelectedCustomer.ZipCode = customer.ZipCode;
            });

            Messenger.Default.Register <Customer>(this, "clear", (customer) =>
            {
                this.ClearControls();
            });

            Messenger.Default.Register <Customer>(this, "reload", (customer) =>
            {
                this.GetCustomer(customer.CustomerID);
            });

            /// <summary>
            /// Get the customer using the customer ID.
            /// </summary>
            GetCustomerCommand = new RelayCommand(() =>
            {
                if (Validator.IsPresent(CustomerIDBox) &&
                    Validator.IsInt32(CustomerIDBox))
                {
                    int customerID = Convert.ToInt32(CustomerIDBox);
                    this.GetCustomer(customerID);
                }
            });

            /// <summary>
            /// Open the add customer view.
            /// </summary>
            AddCustomerCommand = new RelayCommand(() =>
            {
                AddView addView = new AddView();
                addView.Show();
                ButtonIsEnabled = true;
            });

            /// <summary>
            /// Open the modify customer view.
            /// </summary>
            ModifyCustomerCommand = new RelayCommand(() =>
            {
                ModifyView modView = new ModifyView();
                Messenger.Default.Send(SelectedCustomer, "mod");
                modView.Show();
            });

            /// <summary>
            /// Delete a customer from the database.
            /// </summary>
            DeleteCustomerCommand = new RelayCommand(() =>
            {
                try
                {
                    // Mark the row for deletion.
                    // Update the database.
                    MMABooksEntity.mmaBooks.Customers.Remove(selectedCustomer);
                    MMABooksEntity.mmaBooks.SaveChanges();
                    Messenger.Default.Send(new NotificationMessage("Customer Removed!"));

                    CustomerIDBox = "";
                    this.ClearControls();
                }
                // Add concurrency error handling.
                // Place the catch block before the one for a generic exception.
                catch (DbUpdateConcurrencyException ex)
                {
                    ex.Entries.Single().Reload();
                    if (MMABooksEntity.mmaBooks.Entry(selectedCustomer).State == EntityState.Detached)
                    {
                        MessageBox.Show("Another user has deleted " + "that customer.", "Concurrency Error");
                        CustomerIDBox = "";
                        this.ClearControls();
                    }
                    else
                    {
                        MessageBox.Show("Another user has updated " + "that customer.", "Concurrency Error");
                        DisplayCustomer();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, ex.GetType().ToString());
                }

                ButtonIsEnabled = false;
            });

            /// <summary>
            /// Close the current window.
            /// </summary>
            CloseWindowCommand = new RelayCommand <MainWindow>((window) =>
            {
                if (window != null)
                {
                    window.Close();
                }
            });
        }