示例#1
0
        private void ButtonDelete_Click(object sender, RoutedEventArgs e)
        {
            if (ButtonDelete.Content.Equals("Delete"))
            {
                ButtonDelete.Content = "Confirm Delete";
            }
            else
            {
                //delete record
                //Find records in database which matches selected rabbit
                if (rabbit != null)
                {
                    using (var db = new RabbitDbEntities())
                    {
                        var rabbitToDelete = db.Rabbits.Find(rabbit.RabbitId);

                        db.Rabbits.Remove(rabbitToDelete);

                        db.SaveChanges();
                        //Clear list box because we will change it
                        rabbit = null;
                        ListBoxRabbit.ItemsSource = null; //remove binding
                        ListBoxRabbit.Items.Clear();      //clears it

                        //repopulate list box
                        rabbits = db.Rabbits.ToList();
                        ListBoxRabbit.ItemsSource = rabbits;
                    };
                }


                ButtonDelete.Content = "Delete";
            }
        }
示例#2
0
        private void ButtonEdit_Click(object sender, RoutedEventArgs e)
        {
            if (ButtonEdit.Content.Equals("Edit"))
            {
                Color color = (Color)ColorConverter.ConvertFromString("#007ED9");
                var   brush = new SolidColorBrush(color);

                ButtonEdit.Background = brush;
                ButtonEdit.Content    = "Save";

                TextBoxName.IsReadOnly = false;
                TextBoxAge.IsReadOnly  = false;

                ButtonAdd.IsEnabled = false;
                TextBoxName.Focus();
                TextBoxName.SelectAll();
            }
            else
            {
                ButtonEdit.Content = "Edit";
                if ((TextBoxAge.Text.Length > 0) && (TextBoxName.Text.Length > 0))
                {
                    if (rabbit != null)
                    {
                        rabbit.Name = TextBoxName.Text;
                        if (int.TryParse(TextBoxAge.Text, out int age))
                        {
                            rabbit.Age = age;
                        }


                        using (var db = new RabbitDbEntities())
                        {
                            //read rabbit from database by the ID
                            var rabbitToUpdate = db.Rabbits.Find(rabbit.RabbitId);
                            //update the rabbit and save it back to DB
                            rabbitToUpdate.Name = rabbit.Name;
                            rabbitToUpdate.Age  = rabbit.Age;
                            //save it back to db
                            db.SaveChanges();
                            //Clear list box because we will change it
                            rabbit = null;
                            ListBoxRabbit.ItemsSource = null; //remove binding
                            ListBoxRabbit.Items.Clear();      //clears it

                            //repopulate list box
                            rabbits = db.Rabbits.ToList();
                            ListBoxRabbit.ItemsSource = rabbits;
                        }
                    }
                }
                ButtonAdd.IsEnabled = false;
            }
        }
示例#3
0
        private void ButtonAdd_Click(object sender, EventArgs e)
        {
            if (ButtonAdd.Content.Equals("Add"))
            {
                ButtonAdd.Content     = "Save";
                ButtonEdit.Background = (SolidColorBrush)(new BrushConverter().ConvertFrom("#FF1D3A"));
                //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;
                ButtonEdit.IsEnabled   = false;
                ButtonDelete.IsEnabled = false;
            }
            else
            {
                ButtonEdit.Background  = (SolidColorBrush)(new BrushConverter().ConvertFrom("#FBFCFC"));
                ButtonAdd.Content      = "Add";
                TextBoxName.IsReadOnly = true;
                TextBoxAge.IsReadOnly  = true;
                //commit changes
                if (TextBoxName.Text.Length > 0 && TextBoxAge.Text.Length > 0)
                {
                    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();
                            //update view
                            rabbit  = null;
                            rabbits = db.Rabbits.ToList();
                            ListBoxRabbit.Items.Clear();
                            ListBoxRabbit.ItemsSource = rabbits;
                        }
                    }
                }
            }
        }