private void ButtonEdit_Click(object sender, RoutedEventArgs e) { if (ButtonEdit.Content.Equals("Edit")) { ButtonEdit.Background = (SolidColorBrush) new BrushConverter().ConvertFrom("#facdcd"); ButtonEdit.Content = "Save"; TextBoxName.Text = rabbit.Name; TextBoxAge.Text = rabbit.Age.ToString(); TextBoxName.IsReadOnly = false; TextBoxAge.IsReadOnly = false; ListBoxRabbits.IsEnabled = false; //change background ButtonAdd.IsEnabled = false; ButtonDelete.IsEnabled = false; ButtonCancel.IsEnabled = true; TextBoxName.Background = (SolidColorBrush)Brushes.White; TextBoxAge.Background = (SolidColorBrush)Brushes.White; } else { ButtonEdit.Background = (SolidColorBrush) new BrushConverter().ConvertFrom("#bd5555"); ButtonEdit.Content = "Edit"; TextBoxName.IsReadOnly = true; TextBoxAge.IsReadOnly = true; ListBoxRabbits.IsEnabled = true; //change background TextBoxName.Background = (SolidColorBrush) new BrushConverter().ConvertFrom("#cf7c7c"); TextBoxAge.Background = (SolidColorBrush) new BrushConverter().ConvertFrom("#cf7c7c"); if ((TextBoxAge.Text.Length > 0) && (TextBoxName.Text.Length) > 0) { //must have a rabbit selected if (rabbit != null) { rabbit.Name = TextBoxName.Text; if (int.TryParse(TextBoxAge.Text, out int age)) { rabbit.Age = age; } using (var db = new RabbitDbEntities()) { //- read rabbit from database by ID var rabbitToUpdate = db.Rabbits.Find(rabbit.RabbitId); //- Update rabbit rabbitToUpdate.Name = rabbit.Name; rabbitToUpdate.Age = rabbit.Age; //- save rabbit back to database db.SaveChanges(); //- Repopulate the listbox (re-read from the database) rabbits = db.Rabbits.ToList(); //gets rabbits ListBoxRabbits.ItemsSource = rabbits; //binds to listbox } } } } }
private void saveAdditions() { ButtonAdd.Background = (SolidColorBrush) new BrushConverter().ConvertFrom("#bd5555");; ButtonAdd.Content = "Add"; TextBoxName.IsReadOnly = true; TextBoxAge.IsReadOnly = true; ButtonEdit.IsEnabled = true; ButtonDelete.IsEnabled = true; ButtonCancel.IsEnabled = true; TextBoxName.Background = (SolidColorBrush) new BrushConverter().ConvertFrom("#cf7c7c"); TextBoxAge.Background = (SolidColorBrush) new BrushConverter().ConvertFrom("#cf7c7c"); //=== COMMIT CHANGES === if ((TextBoxName.Text.Length > 0) && (TextBoxAge.Text.Length > 0)) { //get the age if (int.TryParse(TextBoxAge.Text, out int age)) { var RabbitToAdd = new Rabbit() { Name = TextBoxName.Text, Age = age }; //read database and add the new rabbit using (var db = new RabbitDbEntities()) { db.Rabbits.Add(RabbitToAdd); db.SaveChanges(); //Update the view rabbits = db.Rabbits.ToList(); //gets rabbits ListBoxRabbits.ItemsSource = rabbits; //binds to listbox } } else { } TextBoxName.Text = ""; TextBoxAge.Text = ""; ButtonEdit.IsEnabled = false; ButtonCancel.IsEnabled = false; ButtonDelete.IsEnabled = false; } }
private void DeleteAllConfirmation() { foreach (Rabbit r in rabbits) { using (var db = new RabbitDbEntities()) { var rabbitToDelete = db.Rabbits.Find(r.RabbitId); db.Rabbits.Remove(rabbitToDelete); db.SaveChanges(); //- Repopulate the listbox (re-read from the database) rabbits = db.Rabbits.ToList(); //gets rabbits ListBoxRabbits.ItemsSource = rabbits; //binds to listbox } } ButtonDeleteAll.Content = "Delete All"; ButtonDelete.IsEnabled = false; ButtonCancel.IsEnabled = false; ListBoxRabbits.IsEnabled = true; ButtonAdd.IsEnabled = true; }