ProductList.xaml.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. namespace Exxxam
  7. {
  8. public partial class ProductList : Page
  9. {
  10. public ProductList()
  11. {
  12. InitializeComponent();
  13. productList.ItemsSource = DataBaseConnection.dataBaseEntities.Products.ToList();
  14. }
  15. private void oldPrice_Loaded(object sender, RoutedEventArgs e)
  16. {
  17. TextBlock oldPrice = (TextBlock)sender;
  18. int indexProduct = Convert.ToInt32(oldPrice.Uid);
  19. Products currentProduct = DataBaseConnection.dataBaseEntities.Products.FirstOrDefault(x => x.Code_Product == indexProduct);
  20. if (currentProduct.Discount_Product > 0)
  21. {
  22. oldPrice.Visibility = Visibility.Visible;
  23. }
  24. }
  25. private void mainPrice_Loaded(object sender, RoutedEventArgs e)
  26. {
  27. TextBlock mainPrice = (TextBlock)sender;
  28. int indexProduct = Convert.ToInt32(mainPrice.Uid);
  29. Products currentProduct = DataBaseConnection.dataBaseEntities.Products.FirstOrDefault(x => x.Code_Product == indexProduct);
  30. if (currentProduct.Discount_Product > 0)
  31. {
  32. mainPrice.Text = $"{currentProduct.Price_Product - (currentProduct.Price_Product * currentProduct.Discount_Product)}";
  33. }
  34. }
  35. private void currentDiscount_Loaded(object sender, RoutedEventArgs e)
  36. {
  37. TextBlock currentDiscount = (TextBlock)sender;
  38. int indexProduct = Convert.ToInt32(currentDiscount.Uid);
  39. Products currentProduct = DataBaseConnection.dataBaseEntities.Products.FirstOrDefault(x => x.Code_Product == indexProduct);
  40. if (currentProduct.Discount_Product > 0)
  41. {
  42. currentDiscount.Text = $"Скидка: {currentProduct.Discount_Product * 100}%";
  43. currentDiscount.Visibility = Visibility.Visible;
  44. }
  45. }
  46. private List<Products> _shoppingCartProduct = new List<Products>();
  47. private void addToShoppingCart_Click(object sender, RoutedEventArgs e)
  48. {
  49. Products selectedProduct = productList.SelectedItem as Products;
  50. bool checkSameProduct = false;
  51. foreach (Products product in _shoppingCartProduct)
  52. {
  53. if (selectedProduct == product)
  54. {
  55. checkSameProduct = true;
  56. product.Count++;
  57. break;
  58. }
  59. }
  60. if (checkSameProduct)
  61. {
  62. checkSameProduct = false;
  63. }
  64. else
  65. {
  66. selectedProduct.Count = 1;
  67. _shoppingCartProduct.Add(selectedProduct);
  68. }
  69. if (_shoppingCartProduct.Count > 0)
  70. {
  71. openShoppingCart.Visibility = Visibility.Visible;
  72. }
  73. }
  74. }
  75. }