Exemple #1
0
        private void ButtonEdit_Click(object sender, RoutedEventArgs e)
        {
            //MessageBox.Show("Hey, We are editing");
            if (ButtonEdit.Content.Equals("Edit"))
            {
                ButtonEdit.Content = "Save";
                //ButtonEdit.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#F6C8B8"));
                //Style style = (Style)Resources["btn"];
                ButtonEdit.Style = (Style)FindResource("red");
                //ButtonEdit.Style = FindResource("red") as Style;

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

                ButtonAdd.IsEnabled = false;
            }
            else
            {
                ButtonEdit.Content    = "Edit";
                ButtonEdit.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#CFE8DC"));
                ButtonEdit.Style      = (Style)FindResource("general");
                //ButtonEdit.Style = Resources["btnRegular"] as Style;

                TextBoxName.IsReadOnly = true;
                TextBoxAge.IsReadOnly  = true;

                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())
                        {
                            Rabbit rabbitToUpdate = db.Rabbits.Find(rabbit.RabbitId);
                            rabbitToUpdate.Name = rabbit.Name;
                            rabbitToUpdate.Age  = rabbit.Age;
                            db.SaveChanges();

                            ListBoxRabbits.ItemsSource = null;
                            ListBoxRabbits.Items.Clear();

                            rabbits = db.Rabbits.ToList();
                            ListBoxRabbits.ItemsSource = rabbits;
                        }
                    }
                }

                ButtonAdd.IsEnabled = true;
            }
        }
        private void ButtonEdit_Click(object sender, RoutedEventArgs e)
        {
            if (ButtonEdit.Content.Equals("Edit"))
            {
                ButtonEdit.Background  = (SolidColorBrush)(new BrushConverter().ConvertFrom("#9589FF"));
                ButtonEdit.Content     = "Save";
                TextBoxName.IsReadOnly = false;
                TextBoxAge.IsReadOnly  = false;
                TextBoxName.Background = (SolidColorBrush)Brushes.White;
                TextBoxAge.Background  = (SolidColorBrush)Brushes.White;
                ButtonAdd.IsEnabled    = false;
                TextBoxName.Focus();
                TextBoxName.SelectAll();
            }
            else
            {
                Color color = (Color)ColorConverter.ConvertFromString("#B3C6ED");
                var   brush = new SolidColorBrush(color);
                ButtonEdit.Background = brush;
                ButtonEdit.Content    = "Edit";
                if ((TextBoxAge.Text.Length > 0) && (TextBoxName.Text.Length > 0))
                {
                    // must have selected a rabbit
                    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 ID
                            var rabbitToUpdate = db.Rabbits.Find(rabbit.RabbitId);
                            // update rabbit
                            rabbitToUpdate.Name = rabbit.Name;
                            rabbitToUpdate.Age  = rabbit.Age;
                            // save rabbit back to DB
                            db.SaveChanges();
                            // clear listbox because we're going to change (the binding)
                            rabbit = null;  // remove binding on rabbit
                            // ListBoxRabbits.ItemsSource = null;  // remove binding
                            // ListBoxRabbits.Items.Clear();       // clear it out
                            // repopulate listbox // re-read from db
                            rabbits = db.Rabbits.ToList();                // get rabbits
                            ListBoxRabbits.ItemsSource = rabbits;         // bind to listbox again
                        }
                    }
                }
                ButtonAdd.IsEnabled = true;
            }
        }
Exemple #3
0
        private void ListBoxRabbits_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (ListBoxRabbits.SelectedItem != null)
            {
                rabbit           = (Rabbit)ListBoxRabbits.SelectedItem;
                TextBoxName.Text = rabbit.Name;
                TextBoxAge.Text  = rabbit.Age.ToString();

                //enable edit and delete
                ButtonEdit.IsEnabled   = true;
                ButtonDelete.IsEnabled = true;
            }
        }
        private void ButtonAdd_Click(object sender, EventArgs e)
        {
            if (ButtonAdd.Content.Equals("Add"))
            {
                ButtonAdd.Content    = "Save";
                ButtonAdd.Background = (SolidColorBrush)(new BrushConverter().ConvertFrom("#9589FF"));
                // 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
            {
                ButtonAdd.Content      = "Add";
                ButtonAdd.Background   = (SolidColorBrush)(new BrushConverter().ConvertFrom("#B3C6ED"));
                TextBoxName.Background = (SolidColorBrush)(new BrushConverter().ConvertFrom("#D3E9ED"));
                TextBoxAge.Background  = (SolidColorBrush)(new BrushConverter().ConvertFrom("#D3E9ED"));
                TextBoxName.IsReadOnly = true;
                TextBoxAge.IsReadOnly  = true;
                // commit changes
                if ((TextBoxName.Text.Length > 0) && (TextBoxAge.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
                        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;
                        }
                    }
                }
            }
        }
 private void ListBoxRabbits_SelectionChanged(object sender, SelectionChangedEventArgs e)
 {
     // when deselect a rabbit, don't run this code
     // if(rabbit != null)  // or
     if (ListBoxRabbits.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)ListBoxRabbits.SelectedItem;
         TextBoxName.Text = rabbit.Name;
         TextBoxAge.Text  = rabbit.Age.ToString();
         // enable edit and delete
         ButtonEdit.IsEnabled   = true;
         ButtonDelete.IsEnabled = true;
     }
 }