//Delete the selected Tab private void btnDeleteTab_Click(object sender, RoutedEventArgs e) { TabViewModel tvm = tabControlName.SelectedItem as TabViewModel; tabViewModels.Remove(tvm); DataContext = tabViewModels; }
//Add the task in selected Category/tab private void btnAddTask_Click(object sender, RoutedEventArgs e) { string task = txt_Task.Text; if (string.IsNullOrEmpty(task)) { MessageBox.Show("Please enter Task to Add", "", MessageBoxButton.OK, MessageBoxImage.Warning); return; } TabViewModel tvm = tabControlName.SelectedItem as TabViewModel; LoadData(tvm, task); }
//Toggle the selected item as Done / UnDone private void btnToggleTask_Click(object sender, RoutedEventArgs e) { if (string.IsNullOrEmpty(lb_selectedItem)) { MessageBox.Show("Please selet the task to Mark as Complete/Not Complete", "Toggle Task", MessageBoxButton.OK, MessageBoxImage.Warning); } TabViewModel tvm = tabControlName.SelectedItem as TabViewModel; List <TabItemViewModel> list_content = new List <TabItemViewModel>(); foreach (TabItemViewModel item in tvm.Collection) { //remove the selected item if (item.TaskName == lb_selectedItem) { if (item.background == Brushes.Green) { item.background = Brushes.Transparent; } else { item.background = Brushes.Green; } } list_content.Add(item); } int index = tabControlName.SelectedIndex; tabViewModels.Remove(tvm); ObservableCollection <TabItemViewModel> obs = new ObservableCollection <TabItemViewModel>(); foreach (TabItemViewModel cont in list_content) { obs.Add(cont); } tabViewModels.Insert(index, new TabViewModel { Name = tvm.Name, Collection = obs }); //tabViewModels. DataContext = tabViewModels; //tabControlName.SelectedItem = tvm; tabControlName.SelectedIndex = index; }
//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(); } }