Esempio n. 1
0
        private void ButtonAdd_Click(object sender, EventArgs e)
        {
            if (ButtonAdd.Content.Equals("Add"))
            {
                ButtonAdd.Content    = "Save";
                ButtonAdd.Background = brush2;
                // clear boxes and set to white
                TextBoxName.Text       = "";
                TextBoxAge.Text        = "";
                TextBoxName.Background = (SolidColorBrush)Brushes.White;
                TextBoxAge.Background  = (SolidColorBrush)Brushes.White;
                TextBoxName.IsReadOnly = false;
                TextBoxAge.IsReadOnly  = false;
            }
            else
            {
                ButtonAdd.Content      = "Add";
                ButtonAdd.Background   = brush3;
                TextBoxAge.Background  = brush2;
                TextBoxName.Background = brush2;
                TextBoxName.IsReadOnly = true;
                TextBoxAge.IsReadOnly  = true;
                // commit changes
                if ((TextBoxAge.Text.Length > 0) && (TextBoxName.Text.Length > 0))
                {
                    // get age
                    if (int.TryParse(TextBoxAge.Text, out int age))
                    {
                        var RabbitToAdd = new Rabbit()
                        {
                            Name = TextBoxName.Text,
                            AGE  = age
                        };
                        // read db and add new rabbit to the list
                        using (var db = new RabbitDbEntities())
                        {
                            db.Rabbits.Add(RabbitToAdd);
                            db.SaveChanges();
                            // to update the view

                            rabbit = null;

                            rabbits = db.Rabbits.ToList();
                            ListBoxRabbit.ItemsSource = null;
                            ListBoxRabbit.ItemsSource = rabbits;
                        }
                    }
                }
            }
        }
Esempio n. 2
0
 private void ListBoxRabbit_SelectionChanged(object sender, SelectionChangedEventArgs e)
 {
     // when delesect a rabbit, don't run this code
     // if(rabbit != null) // or
     if (ListBoxRabbit.SelectedItem != null)
     {
         // whenever we select an item in the list, cast it from (Object) type
         // and put it as a (Rabbit) type. Put in the global 'rabbit' variable
         rabbit           = (Rabbit)ListBoxRabbit.SelectedItem;
         TextBoxName.Text = rabbit.Name;
         TextBoxAge.Text  = rabbit.AGE.ToString();
         // enable edit and delete
         ButtonEdit.IsEnabled   = true;
         ButtonDelete.IsEnabled = true;
     }
 }