private void ButtonAdd_Click(object sender, EventArgs e) { //MessageBox.Show("Hey, we're adding homie"); if (ButtonAdd.Content.Equals("Add")) { ButtonAdd.Content = "Save"; ButtonAdd.Background = (SolidColorBrush)(new BrushConverter().ConvertFrom("#FF1D3A")); TextBoxName.Text = ""; TextBoxAge.Text = ""; TextBoxName.Background = (SolidColorBrush)Brushes.White; TextBoxAge.Background = (SolidColorBrush)Brushes.White; TextBoxName.IsReadOnly = false; TextBoxAge.IsReadOnly = false; ButtonEdit.IsEnabled = false; ButtonDel.IsEnabled = false; TextBoxName.Focus(); } else { ButtonAdd.Content = "Add"; ButtonAdd.Background = (SolidColorBrush)(new BrushConverter().ConvertFrom("#E8C3E3")); //Clear out boxes, set to white TextBoxName.Background = (SolidColorBrush)(new BrushConverter().ConvertFrom("#FFD6E5")); TextBoxAge.Background = (SolidColorBrush)(new BrushConverter().ConvertFrom("#FFD6E5")); TextBoxName.IsReadOnly = true; TextBoxAge.IsReadOnly = true; if (TextBoxName.Text.Length > 0 && TextBoxAge.Text.Length > 0) { //get age if (int.TryParse(TextBoxAge.Text, out int age)) { var RabbitToAdd = new Rabbit() { Name = TextBoxName.Text, Age = age }; using (var db = new RabbitDbEntities()) { db.Rabbits.Add(RabbitToAdd); db.SaveChanges(); rabbits = db.Rabbits.ToList(); listBoxRabbits.ItemsSource = rabbits; } } } } }
private void ListBoxRabbits_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (listBoxRabbits.SelectedItem != null) { //Whenever we select an item in the list, cast it from type object to type RABBIT. Place it in the global 'rabbit' variable. rabbit = (Rabbit)listBoxRabbits.SelectedItem; //MessageBox.Show(rabbit.Name); TextBoxName.Text = rabbit.Name; TextBoxAge.Text = rabbit.Age.ToString(); //enable edit and delete ButtonEdit.IsEnabled = true; ButtonDel.IsEnabled = true; } }