예제 #1
0
 private void ButtonDelete_Click(object sender, RoutedEventArgs e)
 {
     if (ButtonDelete.Content.ToString() == "Delete")
     {
         ButtonDelete.Content          = "Confirm";
         TextboxDescription.Background = Brushes.White;
         TextboxCategoryId.Background  = Brushes.White;
         TextboxDescription.IsReadOnly = false;
         TextboxCategoryId.IsReadOnly  = false;
     }
     else
     {
         using (var db = new TasksDBEntities())
         {
             var taskToDelete = db.Tasks.Find(task.TaskID);
             db.Tasks.Remove(taskToDelete);
             db.SaveChanges();
             ListBoxTasks.ItemsSource = null;  //  reset list box
             tasks = db.Tasks.ToList();        // get fresh link
             ListBoxTasks.ItemsSource = tasks; // re-link
         }
         ButtonDelete.Content    = "Delete";
         ButtonDelete.IsEnabled  = false;
         TextboxId.Text          = " ";
         TextboxDescription.Text = " ";
         TextboxCategoryId.Text  = " ";
     }
 }
예제 #2
0
 private void ButtonEdit_Click(object sender, RoutedEventArgs e)
 {
     if (ButtonEdit.Content.ToString() == "Edit")
     {
         TextboxId.IsReadOnly          = true;
         TextboxDescription.IsReadOnly = false;
         TextboxCategoryId.IsReadOnly  = false;
         ButtonEdit.IsEnabled          = true;
         ButtonEdit.Content            = "Save";
         var brush = new BrushConverter();
         TextboxDescription.Background = (Brush)brush.ConvertFrom("#0A84FF");
     }
     else
     {
         using (var db = new TasksDBEntities())
         {
             //update description and category id
             var taskToEdit = db.Tasks.Find(task.TaskID);
             taskToEdit.Description = TextboxDescription.Text;
             //convert category id to integer from textbox
             //try parse safe way to do converstion, null if fails
             int.TryParse(TextboxCategoryId.Text, out int categoryId);
             taskToEdit.CategoryID = categoryId;
             //update record in database
             db.SaveChanges();
             //update list box
             ListBoxTasks.ItemsSource = null; //reset listbox
             tasks = db.Tasks.ToList();       //
             ListBoxTasks.ItemsSource = tasks;
         }
         TextboxId.IsReadOnly          = true;
         TextboxDescription.IsReadOnly = true;
         TextboxCategoryId.IsReadOnly  = true;
         ButtonEdit.Content            = "Edit";
         ButtonEdit.IsEnabled          = false;
         var brush = new BrushConverter();
         TextboxDescription.Background = (Brush)brush.ConvertFrom("#0A84FF");
     }
 }