//Delete the selected Task private void btnDeleteTask_Click(object sender, RoutedEventArgs e) { if (string.IsNullOrEmpty(lb_selectedItem)) { MessageBox.Show("Please select a task to delete.", "Delete Task", MessageBoxButton.OK, MessageBoxImage.Warning); return; } //get the selected tab item TabViewModel tvm = tabControlName.SelectedItem as TabViewModel; //get the list of contents from the tab List <TabItemViewModel> list_content = new List <TabItemViewModel>(); foreach (TabItemViewModel item in tvm.Collection) { //remove the selected item if (item.TaskName.Length > 0 && item.TaskName != lb_selectedItem) { TabItemViewModel tiv = new TabItemViewModel(); tiv.TaskName = item.TaskName; tiv.background = item.background; list_content.Add(tiv); } } int index = tabControlName.SelectedIndex; //remove the task tabViewModels.Remove(tvm); ObservableCollection <TabItemViewModel> obs = new ObservableCollection <TabItemViewModel>(); foreach (TabItemViewModel cont in list_content) { obs.Add(cont); } //update the new list of contents in the tabcontrol tabViewModels.Insert(index, new TabViewModel { Name = tvm.Name, Collection = obs }); //tabViewModels. DataContext = tabViewModels; tabControlName.SelectedIndex = index; }
private void LoadData(TabViewModel tvm, string task) { int index = tabControlName.SelectedIndex; //get the current Tab context List <TabItemViewModel> tabContents = new List <TabItemViewModel>(); foreach (TabItemViewModel item in tvm.Collection) { if (item.TaskName.Length > 0) { TabItemViewModel tvi = new TabItemViewModel(); tvi.TaskName = item.TaskName; tvi.background = item.background; tabContents.Add(tvi); } } //new task TabItemViewModel tvi2 = new TabItemViewModel(); tvi2.TaskName = task; tvi2.background = Brushes.Transparent; //add the new item to the contents list tabContents.Add(tvi2); //Remove the tab and add the new list tabViewModels.Remove(tvm); ObservableCollection <TabItemViewModel> obs = new ObservableCollection <TabItemViewModel>(); foreach (TabItemViewModel cont in tabContents) { obs.Add(cont); } //add the tab in the same index tabViewModels.Insert(index, new TabViewModel { Name = tvm.Name, Collection = obs }); DataContext = tabViewModels; //select the same tab tabControlName.SelectedIndex = index; }
private void ImportFromExcel(string path) { try { Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application(); Microsoft.Office.Interop.Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(path); Microsoft.Office.Interop.Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1]; Microsoft.Office.Interop.Excel.Range xlRange = xlWorksheet.UsedRange; tabViewModels.Clear(); List <ExcelData> datalist = new List <ExcelData>(); //skip row 1 for header for (int i = 2; i <= xlRange.Rows.Count; i++) { ExcelData data = new ExcelData(); for (int j = 1; j <= xlRange.Columns.Count; j++) { Microsoft.Office.Interop.Excel.Range range = (xlWorksheet.Cells[i, j] as Microsoft.Office.Interop.Excel.Range); string cellValue = range.Value.ToString(); if (j == 1) { data.Tab = cellValue; } if (j == 2) { data.Task = cellValue; } if (j == 3) { if (cellValue == "Completed") { data.Status = cellValue; //tivm.background = Brushes.Green; } else { data.Status = cellValue; //tivm.background = Brushes.Transparent; } } } datalist.Add(data); } List <string> uniquetabs = new List <string>(); foreach (ExcelData d in datalist) { if (!uniquetabs.Contains(d.Tab)) { uniquetabs.Add(d.Tab); } } foreach (string tab in uniquetabs) { TabViewModel tvm = new TabViewModel(); ObservableCollection <TabItemViewModel> obs = new ObservableCollection <TabItemViewModel>(); foreach (ExcelData d in datalist) { if (d.Tab == tab) { TabItemViewModel tiv = new TabItemViewModel(); tiv.TaskName = d.Task; if (d.Status == "Completed") { tiv.background = Brushes.Green; } else { tiv.background = Brushes.Transparent; } obs.Add(tiv); } } tvm.Name = tab; tvm.Collection = obs; tabViewModels.Add(tvm); } xlWorkbook.Close(); xlApp.Quit(); DataContext = tabViewModels; tabControlName.SelectedIndex = 1; } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); } }