コード例 #1
0
 private void ListViewRabbits_SelectionChanged(object sender, SelectionChangedEventArgs e)
 {
     if (ListViewRabbits.SelectedItem != null)
     {
         Rabbit = (rabbittable)ListViewRabbits.SelectedItem;
         TextBoxRabbitID.Text         = Rabbit.RabbitTableID.ToString();
         TextBoxRabbitName.Text       = Rabbit.RabbitName.ToString();
         TextBoxRabbitAge.Text        = Rabbit.RabbitAge.ToString();
         TextBoxRabbitID.IsReadOnly   = true;
         TextBoxRabbitName.IsReadOnly = true;
         TextBoxRabbitAge.IsReadOnly  = true;
     }
     ButtonEdit.IsEnabled = true;
 }
コード例 #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";
            }
        }