예제 #1
0
        private void ButtonAdd_Click(object sender, EventArgs e)
        {
            if (ButtonAdd.Content.Equals("Add"))
            {
                ButtonAdd.Background   = (SolidColorBrush)(new BrushConverter().ConvertFrom("#D9C216"));
                ButtonAdd.Content      = "Save";
                TextBoxName.Text       = "";
                AgeBoxName.Text        = "";
                TextBoxName.IsReadOnly = false;
                AgeBoxName.IsReadOnly  = false;
                ButtonEdit.IsEnabled   = false;
                ButtonDelete.IsEnabled = false;
                //ButtonAdd.IsEnabled = false;
            }
            else
            {
                {
                    ButtonAdd.Content      = "Add";
                    ButtonAdd.Background   = (SolidColorBrush)(new BrushConverter().ConvertFrom("#B3C6ED"));
                    TextBoxName.Background = (SolidColorBrush)(new BrushConverter().ConvertFrom("#D3E9ED"));
                    AgeBoxName.Background  = (SolidColorBrush)(new BrushConverter().ConvertFrom("#D3E9ED"));
                    TextBoxName.IsReadOnly = true;
                    AgeBoxName.IsReadOnly  = true;
                    // commit changes
                    if ((TextBoxName.Text.Length > 0) && (AgeBoxName.Text.Length > 0))
                    {
                        // get age
                        if (int.TryParse(AgeBoxName.Text, out int age))
                        {
                            var RabbitToAdd = new Rabbit()
                            {
                                Name = TextBoxName.Text,
                                Age  = age
                            };
                            // read db and add new rabbit
                            using (var db = new RabbitDbEntities())
                            {
                                db.Rabbits.Add(RabbitToAdd);
                                db.SaveChanges();
                                // update view
                                rabbit = null;

                                rabbits = db.Rabbits.ToList();
                                ListBoxRabbits.ItemsSource = null;
                                ListBoxRabbits.ItemsSource = rabbits;
                            }
                        }
                    }
                    ButtonAdd.IsEnabled = true;
                }
            }
        }
예제 #2
0
        private void ButtonEdit_Click(object sender, RoutedEventArgs e)
        {
            if (ButtonEdit.Content.Equals("Edit"))
            {
                ButtonEdit.Background  = (SolidColorBrush)(new BrushConverter().ConvertFrom("#D9C216"));
                ButtonEdit.Content     = "Save";
                TextBoxName.IsReadOnly = false;
                AgeBoxName.IsReadOnly  = false;
            }
            else
            {
                ButtonEdit.Content = "Edit";
                if ((TextBoxName.Text.Length > 0) && (AgeBoxName.Text.Length > 0))
                {
                    if (rabbit != null)
                    {
                        rabbit.Name = TextBoxName.Text;
                        // rabbit.Age = AgeBoxName.Text.ToString();
                        if (int.TryParse(AgeBoxName.Text, out int age))
                        {
                            rabbit.Age = age;
                        }

                        using (var db = new RabbitDbEntities())
                        {
                            // read rabbit from database by ID,
                            var rabbitToUpdate = db.Rabbits.Find(rabbit.RabbitID);
                            //update rabbit
                            rabbitToUpdate.Name = rabbit.Name;
                            rabbitToUpdate.Age  = rabbit.Age;
                            //save rabbit to db
                            db.SaveChanges();
                            rabbit = null;
                            // clear listbox
                            ListBoxRabbits.ItemsSource = null;    // remove binding
                            ListBoxRabbits.Items.Clear();         // clear it out
                            // repopulate listbox
                            rabbits = db.Rabbits.ToList();        // get rabbits
                            ListBoxRabbits.ItemsSource = rabbits; // bind to list again
                        }
                    }
                }
            }
        }
예제 #3
0
        void Initialise()
        {
            // using = auto clean up c# will clear the memory
            using (var db = new RabbitDbEntities())
            {
                rabbits = db.Rabbits.ToList();
            }

            // manual method
            //rabbits.ForEach(rabbit => ListBoxRabbits.Items.Add(rabbit))

            // Binding the listbox to the database
            ListBoxRabbits.ItemsSource = rabbits;

            TextBoxName.IsReadOnly = true;
            AgeBoxName.IsReadOnly  = true;

            ButtonEdit.IsEnabled   = false;
            ButtonDelete.IsEnabled = false;
        }