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 ListBoxRabbits_SelectionChanged(object sender, SelectionChangedEventArgs e) { //MessageBox.Show(rabbit.Name); if (ListBoxRabbits.SelectedItem != null) { rabbit = (Rabbit)ListBoxRabbits.SelectedItem; NameBox.Text = rabbit.Name; AgeBox.Text = rabbit.Age.ToString(); EditButton.IsEnabled = true; DeleteButton.IsEnabled = true; AddButton.IsEnabled = true; } else { EditButton.IsEnabled = false; DeleteButton.IsEnabled = false; } }