Example #1
0
        //Create component(s)
        public void CreateComponentWindow()
        {
            WaitForLoanInformationUpdated();

            if (SelectedItem != null)
            {
                //Copy the selected into the new view, for easy adding of more of the same
                newComponent_ = new Component(SelectedItem);
                newComponent_.ComponentNumber = GetNextComponentNumber(SelectedItem.ComponentName);
            }
            else
            {
                newComponent_ = new Component {
                    ComponentNumber = 1
                }
            };

            //Remove "Null" text when creating new component
            newComponent_.ActualLoanInformation.LoanDate   = null;
            newComponent_.ActualLoanInformation.ReturnDate = null;

            //Locator locate our CreateComponentViewModel, then set the CreateComponentViewModel equal to a new one with newComponent_ inside:
            var locator = (ViewModelLocator)Application.Current.FindResource("Locator");

            locator.CreateComponentViewModel = new CreateComponentViewModel(newComponent_);
            locator.CreateComponentViewModel.AddThisManyComponents = 1;

            Window dlg = new CreateComponent();

            dlg.DataContext           = locator.CreateComponentViewModel;
            dlg.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            bool tryedReconnect = false;             // Makes sure the reconnect message only appears once

            //If user press ok then add all the components
            if (dlg.ShowDialog() == true)
            {
                if (!string.IsNullOrEmpty(locator.CreateComponentViewModel.NewComponent.ComponentName))
                {
                    var startNumber = locator.CreateComponentViewModel.NewComponent.ComponentNumber;
                    for (int i = 0; i < locator.CreateComponentViewModel.AddThisManyComponents; i++)
                    {
                        var tempComponent = new Component(newComponent_);            //copy the newComponent
                        tempComponent.ComponentNumber = startNumber + i;             //change the component number accordingly

                        if (false == cdbUtil_.CreateComponent(tempComponent))
                        {
                            if (!tryedReconnect)
                            {
                                Reconnect();
                                tryedReconnect = true;
                            }
                        }
                        AllComponentList.Add(tempComponent);
                    }
                }
                else
                {
                    MessageBox.Show("Du skal indtaste et komponent-navn.");
                }
            }

            SetComponentList(AllComponentList);             //Set the content of the componentdatagrid
            CategoryIndex = 0;
            //then or else dischard the data the user inputted:
            newComponent_ = null;             //redundant? Don't ref the old one, rather some new uninitted
        }