ProductList.xaml.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. namespace SergechevExam
  7. {
  8. /// <summary>
  9. /// Логика взаимодействия для ProductList.xaml
  10. /// </summary>
  11. public partial class ProductList : Page
  12. {
  13. public ProductList()
  14. {
  15. InitializeComponent();
  16. productList.ItemsSource = DataBaseConnection.dataBaseEntities.Products.ToList();
  17. }
  18. private void oldPrice_Loaded(object sender, RoutedEventArgs e) // Проверка на наличие скидки у товара. Если она есть, то отрисовывается перечеркнутый TB, в котором старая цена
  19. {
  20. TextBlock oldPrice = (TextBlock)sender;
  21. int indexProduct = Convert.ToInt32(oldPrice.Uid);
  22. Products currentProduct = DataBaseConnection.dataBaseEntities.Products.FirstOrDefault(x => x.Code_Product == indexProduct);
  23. if (currentProduct.Discount_Product != 0)
  24. {
  25. oldPrice.Visibility = Visibility.Visible;
  26. }
  27. }
  28. private void mainPrice_Loaded(object sender, RoutedEventArgs e) // Проверка на наличие скидки у товара. Если она есть, то рядом с перечеркнутым TB отрисовывается новая цена
  29. {
  30. TextBlock mainPrice = (TextBlock)sender;
  31. int indexProduct = Convert.ToInt32(mainPrice.Uid);
  32. Products currentProduct = DataBaseConnection.dataBaseEntities.Products.FirstOrDefault(x => x.Code_Product == indexProduct);
  33. if (currentProduct.Discount_Product > 0)
  34. {
  35. mainPrice.Text = $"{currentProduct.Price_Product - (currentProduct.Price_Product * currentProduct.Discount_Product)}";
  36. }
  37. }
  38. private void discount_Loaded(object sender, RoutedEventArgs e)
  39. {
  40. TextBlock discount = (TextBlock)sender;
  41. int indexProduct = Convert.ToInt32(discount.Uid);
  42. Products currentProduct = DataBaseConnection.dataBaseEntities.Products.FirstOrDefault(x => x.Code_Product == indexProduct);
  43. if (currentProduct.Discount_Product != 0)
  44. {
  45. discount.Visibility = Visibility.Visible;
  46. discount.Text = $"Скидка: {Convert.ToInt32(currentProduct.Discount_Product*100)}%";
  47. }
  48. }
  49. private List<Products> _shoppingCartProduct = new List<Products>();
  50. private void addToCart_Click(object sender, RoutedEventArgs e)
  51. {
  52. Products selectedProduct = productList.SelectedItem as Products;
  53. bool checkSameProduct = false;
  54. foreach (Products product in _shoppingCartProduct)
  55. {
  56. if (selectedProduct == product)
  57. {
  58. checkSameProduct = true;
  59. product.Count += 1;
  60. break;
  61. }
  62. }
  63. if (checkSameProduct == false)
  64. {
  65. selectedProduct.Count = 1;
  66. _shoppingCartProduct.Add(selectedProduct);
  67. }
  68. else
  69. {
  70. checkSameProduct = false;
  71. }
  72. if (_shoppingCartProduct.Count > 0)
  73. {
  74. openShoppingCart.Visibility = Visibility.Visible;
  75. }
  76. else
  77. {
  78. openShoppingCart.Visibility = Visibility.Collapsed;
  79. }
  80. }
  81. private void openShoppingCart_Click(object sender, RoutedEventArgs e)
  82. {
  83. ShoppingCart shoppingCart = new ShoppingCart(_shoppingCartProduct);
  84. shoppingCart.ShowDialog();
  85. }
  86. }
  87. }