private void EditFunction() { if (EditButton.Content.Equals("Edit")) { EditButton.Content = "Save"; EditButton.Background = (SolidColorBrush)(new BrushConverter().ConvertFrom("#802716")); NameBox.IsReadOnly = false; AgeBox.IsReadOnly = false; NameBox.Background = (SolidColorBrush)(new BrushConverter().ConvertFrom("#FF8E78")); AgeBox.Background = (SolidColorBrush)(new BrushConverter().ConvertFrom("#FF8E78")); AddButton.IsEnabled = false; DeleteButton.IsEnabled = false; ListBoxRabbits.IsEnabled = false; NameBox.Focus(); NameBox.SelectAll(); } else { EditButton.Content = "Edit"; EditButton.Background = (SolidColorBrush)(new BrushConverter().ConvertFrom("#80473C")); NameBox.IsReadOnly = true; AgeBox.IsReadOnly = true; NameBox.Background = (SolidColorBrush)(new BrushConverter().ConvertFrom("#CC3F23")); AgeBox.Background = (SolidColorBrush)(new BrushConverter().ConvertFrom("#CC3F23")); if (AgeBox.Text.Length > 0 && NameBox.Text.Length > 0 && (NameBox.Text.Length.Equals(null) == false) && (AgeBox.Text.Length.Equals(null) == false)) { //must have selected a rabbit if (rabbit != null) { rabbit.Name = NameBox.Text; if (int.TryParse(AgeBox.Text, out int age)) { rabbit.Age = age; } //Read rabbit from database by the id using (var db = new RabbitDbEntities()) { var rabbitToUpdate = db.Rabbits.Find(rabbit.RabbitID); //update the rabbit rabbitToUpdate.Name = rabbit.Name; rabbitToUpdate.Age = rabbit.Age; //save the rabbit back to database db.SaveChanges(); //repop the list box ListBoxRabbits.ItemsSource = db.Rabbits.ToList(); } } NameBox.Text = ""; AgeBox.Text = ""; } AddButton.IsEnabled = true; ListBoxRabbits.IsEnabled = true; } }
private void AddFunction() { if (AddButton.Content.Equals("Add")) { AddButton.Content = "Save"; AddButton.Background = (SolidColorBrush)(new BrushConverter().ConvertFrom("#802716")); NameBox.IsReadOnly = false; AgeBox.IsReadOnly = false; NameBox.Text = ""; AgeBox.Text = ""; NameBox.Background = (SolidColorBrush)(new BrushConverter().ConvertFrom("#FF8E78")); AgeBox.Background = (SolidColorBrush)(new BrushConverter().ConvertFrom("#FF8E78")); EditButton.IsEnabled = false; DeleteButton.IsEnabled = false; NameBox.Focus(); ImageJig.Opacity = 100d; System.Threading.Thread.Sleep(200); ImageJig.Opacity = 0d; } else { AddButton.Content = "Add"; AddButton.Background = (SolidColorBrush)(new BrushConverter().ConvertFrom("#80473C")); NameBox.IsReadOnly = true; AgeBox.IsReadOnly = true; NameBox.Background = (SolidColorBrush)(new BrushConverter().ConvertFrom("#CC3F23")); AgeBox.Background = (SolidColorBrush)(new BrushConverter().ConvertFrom("#CC3F23")); if (AgeBox.Text.Length > 0 && NameBox.Text.Length > 0 && (NameBox.Text.Length.Equals(null) == false) && (AgeBox.Text.Length.Equals(null) == false)) { //must have selected a rabbit var rabbitToAdd = new Rabbit(); if (rabbit != null) { rabbitToAdd.Name = NameBox.Text; if (int.TryParse(AgeBox.Text, out int age)) { rabbitToAdd.Age = age; } //Read rabbit from database by the id using (var db = new RabbitDbEntities()) { //update the rabbit db.Rabbits.Add(rabbitToAdd); //save the rabbit back to database db.SaveChanges(); //repop the list box ListBoxRabbits.ItemsSource = db.Rabbits.ToList(); } } } } }
private void DeleteFunction() { using (var db = new RabbitDbEntities()) { //update the rabbit var rabbitToDelete = db.Rabbits.Find(rabbit.RabbitID); db.Rabbits.Remove(rabbitToDelete); //save the rabbit back to database db.SaveChanges(); //repop the list box ListBoxRabbits.ItemsSource = db.Rabbits.ToList(); NameBox.Text = ""; AgeBox.Text = ""; } }