public MainWindowViewModel() { // Connect to database _DBGateway = new DatabaseGateway($"Data Source={App.databasePath};"); // Load all data _DBGateway.LoadAllData(); // Convert data to VMs ObservableCollection <TaskViewModel> taskItems = new ObservableCollection <TaskViewModel>(); ObservableCollection <WBSViewModel> wbsItems = new ObservableCollection <WBSViewModel>(); foreach (WBS wbsItem in _DBGateway.WBSs) { WBSViewModel wbsVM = new WBSViewModel(wbsItem, _DBGateway); wbsItems.Add(wbsVM); } foreach (TaskItem taskItem in _DBGateway.TaskItems) { TaskViewModel taskVM = new TaskViewModel(taskItem, _DBGateway); if (taskItem.WBSId != null) { foreach (WBSViewModel wbsVM in wbsItems) { if (taskItem.WBSId == wbsVM.WBSItem.WBSId) { taskVM.WBSVM = wbsVM; break; } } } taskItems.Add(taskVM); } TimerAsync ta = new TimerAsync(); ta.StartTimer(); // Add available pages PageViewModels.Add("task", new TaskPageViewModel(_DBGateway, taskItems, wbsItems, ta)); PageViewModels.Add("wbs", new WBSPageViewModel(_DBGateway, wbsItems, taskItems)); // Set starting page CurrentPageViewModel = PageViewModels["task"]; }
public void DeleteWBS(WBSViewModel wbs) { // Check if currently used by task foreach (TaskViewModel taskVM in TaskViewModels) { if (taskVM.WBSVM == null) { continue; } else if (taskVM.WBSVM.WBSItem.WBSId == wbs.WBSItem.WBSId) { return; } } // Delete task from database _dbGateway.DeleteWBS(wbs.WBSItem); // Remove task from collection WBSViewModels.Remove(wbs); }