示例#1
0
 private void ListViewRabbits_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
 {
     if (Rabbit != null)
     {
         using (var db = new newrabbitdatabaseEntities())
         {
             var rabbitToDelete = db.rabbittables.Find(Rabbit.RabbitTableID);
             var result         = MessageBox.Show($"Delete {Rabbit.RabbitName}? Are you sure?");
             if (result == MessageBoxResult.OK)
             {
                 db.rabbittables.Remove(rabbitToDelete);
                 db.SaveChanges();
                 ListViewRabbits.ItemsSource = null;
                 rabbits = db.rabbittables.ToList();
                 ListViewRabbits.ItemsSource = rabbits;
             }
         }
     }
 }
示例#2
0
        private void ButtonAddRabbits_Click(object sender, RoutedEventArgs e)
        {
            if (ButtonAddRabbits.Content.ToString() == "Add Rabbit")
            {
                TextBoxRabbitName.IsReadOnly = false;
                TextBoxRabbitAge.IsReadOnly  = false;
                TextBoxRabbitID.Text         = "";
                TextBoxRabbitName.Text       = "";
                TextBoxRabbitAge.Text        = "";
                ButtonAddRabbits.Content     = "Save";
            }
            else
            {
                if (TextBoxRabbitName.Text.Length > 0)
                {
                    Int32.TryParse(TextBoxRabbitAge.Text, out int rabbitAge);
                    var rabbitToAdd = new rabbittable()
                    {
                        RabbitName = TextBoxRabbitName.Text,
                        RabbitAge  = rabbitAge,
                    };
                    using (var db = new newrabbitdatabaseEntities())
                    {
                        db.rabbittables.Add(rabbitToAdd);
                        db.SaveChanges();
                        ListViewRabbits.ItemsSource = null;
                        rabbits = db.rabbittables.ToList();
                        ListViewRabbits.ItemsSource = rabbits;
                    }
                }

                TextBoxRabbitName.IsReadOnly = true;
                TextBoxRabbitAge.IsReadOnly  = true;
                ButtonAddRabbits.Content     = "Add Rabbit";
            }
        }