Add.xaml.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Data;
  10. using System.Windows.Documents;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Imaging;
  14. using System.Windows.Navigation;
  15. using System.Windows.Shapes;
  16. namespace SneakersSkakunov
  17. {
  18. enum StateOfPage { Create, Update };
  19. /// <summary>
  20. /// Логика взаимодействия для AddPage.xaml
  21. /// </summary>
  22. public partial class Add : Page
  23. {
  24. string path;
  25. StateOfPage stateOfPage;
  26. Sniker DB = new Sniker();
  27. Sneakers snk = new Sneakers();
  28. void addList()
  29. {
  30. Brands.ItemsSource = DB.Brand.ToList();
  31. Brands.DisplayMemberPath = "Label";
  32. Brands.SelectedValuePath = "id_Brand";
  33. }
  34. public Add()
  35. {
  36. InitializeComponent();
  37. addList();
  38. stateOfPage = StateOfPage.Create;
  39. }
  40. public Add(int index)
  41. {
  42. InitializeComponent();
  43. BtnAdd.Content = "Внести изменения";
  44. addList();
  45. stateOfPage = StateOfPage.Update;
  46. //stateOfPage = StateOfPage.Update;
  47. snk = DB.Sneakers.FirstOrDefault(x => x.id_Sneakers == index);
  48. Brand brd = DB.Brand.FirstOrDefault(x => x.id_Brand == snk.id_Brand);
  49. Brands.SelectedItem = brd;
  50. Clrs.Text = snk.Colors;
  51. Prices.Text = snk.Price.ToString();
  52. Sizes.Text = snk.Size;
  53. }
  54. private void BtnAdd_Click(object sender, RoutedEventArgs e)
  55. {
  56. //try
  57. //{
  58. snk.id_Brand = ((Brand)Brands.SelectedItem).id_Brand;
  59. snk.Colors = Clrs.Text;
  60. snk.Price = Convert.ToDecimal(Prices.Text);
  61. snk.Size = Sizes.Text;
  62. if (stateOfPage == StateOfPage.Create)
  63. {
  64. DB.Sneakers.Add(snk);
  65. DB.SaveChanges();
  66. }
  67. else
  68. {
  69. DB.SaveChanges();
  70. }
  71. MessageBox.Show("Вы успешно добавили (обновили) список брэндов");
  72. //}
  73. //catch
  74. //{
  75. // MessageBox.Show("Что-то пошло не так");
  76. // System.Windows.Application.Current.Shutdown();
  77. //}
  78. }
  79. private void Revert(object sender, RoutedEventArgs e)
  80. {
  81. MainFrame.mframe.Navigate(new Sneaker());
  82. }
  83. }
  84. }