MainWindow.xaml.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15. namespace ProjectD
  16. {
  17. /// <summary>
  18. /// Логика взаимодействия для MainWindow.xaml
  19. /// </summary>
  20. public partial class MainWindow : Window
  21. {
  22. private B0Entities _entities;
  23. public List<Ingredient> Ingredients { get; set; }
  24. public MainWindow()
  25. {
  26. InitializeComponent();
  27. _entities = new B0Entities();
  28. Ingredients = _entities.Ingredient.ToList();
  29. }
  30. private void Button_Click_1(object sender, RoutedEventArgs e)
  31. {
  32. StackPanel stackPanel = new StackPanel();
  33. stackPanel.Orientation = Orientation.Horizontal;
  34. IngredientList.Children.Add(stackPanel);
  35. ComboBox comboBox = new ComboBox();
  36. TextBox textBox = new TextBox();
  37. textBox.Width = 40;
  38. comboBox.Width = 120;
  39. comboBox.ItemsSource = Ingredients;
  40. comboBox.DisplayMemberPath = "name";
  41. stackPanel.Children.Add(comboBox);
  42. stackPanel.Children.Add(textBox);
  43. }
  44. private void Button_Click(object sender, RoutedEventArgs e)
  45. {
  46. if (textBoxDishName.Text.Length == 0)
  47. {
  48. textBoxDishName.Focus();
  49. MessageBox.Show("Введите название блюда");
  50. return;
  51. }
  52. Dish dish = new Dish()
  53. {
  54. name = textBoxDishName.Text,
  55. };
  56. var d = _entities.Dish.Add(dish);
  57. _entities.SaveChanges();
  58. List<DishCompound> list = new List<DishCompound>();
  59. foreach (var ing in IngredientList.Children)
  60. {
  61. var st = ing as StackPanel;
  62. if (st != null)
  63. {
  64. var cb = st.Children[0] as ComboBox;
  65. var tb = st.Children[1] as TextBox;
  66. var ingredient = cb.SelectedItem as Ingredient;
  67. if (ingredient != null)
  68. {
  69. list.Add(new DishCompound()
  70. {
  71. dish = d.id,
  72. ingredient = ingredient.id,
  73. volume = float.Parse(tb.Text),
  74. });
  75. }
  76. }
  77. }
  78. _entities.DishCompound.AddRange(list);
  79. _entities.SaveChanges();
  80. }
  81. }
  82. }