UpdateProductWindow.xaml.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Data;
  11. using System.Windows.Documents;
  12. using System.Windows.Input;
  13. using System.Windows.Media;
  14. using System.Windows.Media.Imaging;
  15. using System.Windows.Shapes;
  16. namespace Eight
  17. {
  18. /// <summary>
  19. /// Логика взаимодействия для UpdateProductWindow.xaml
  20. /// </summary>
  21. public partial class UpdateProductWindow : Window
  22. {
  23. private string _path;
  24. public UpdateProductWindow(Product product)
  25. {
  26. InitializeComponent();
  27. Load();
  28. DataContext = product;
  29. foreach(var productMaterial in product.ProductMaterial)
  30. {
  31. BasketBox.Items.Add(productMaterial);
  32. }
  33. _path = product.ImagePath;
  34. Img.Source = GetBitmapImage(_path);
  35. }
  36. private BitmapImage GetBitmapImage(string path)
  37. {
  38. BitmapImage bitmapImage = new BitmapImage();
  39. bitmapImage.BeginInit();
  40. bitmapImage.UriSource = new Uri(path);
  41. bitmapImage.EndInit();
  42. return bitmapImage;
  43. }
  44. private void Load()
  45. {
  46. CmbType.ItemsSource = Helper.GetContext().ProductType.ToList();
  47. ProductsBox.ItemsSource = Helper.GetContext().Material.ToList();
  48. }
  49. private void BtnChoose_Click(object sender, RoutedEventArgs e)
  50. {
  51. try
  52. {
  53. OpenFileDialog openFileDialog = new OpenFileDialog();
  54. if (openFileDialog.ShowDialog() == true)
  55. {
  56. var fileName = openFileDialog.FileName;
  57. _path = "\\products\\" + openFileDialog.SafeFileName;
  58. File.Copy(fileName, Environment.CurrentDirectory + "\\products\\" + openFileDialog.SafeFileName, true);
  59. Img.Source = GetBitmapImage(Environment.CurrentDirectory + "\\products\\" + openFileDialog.SafeFileName);
  60. }
  61. }
  62. catch (Exception)
  63. {
  64. return;
  65. }
  66. }
  67. private void BtnAdd_Click(object sender, RoutedEventArgs e)
  68. {
  69. if (ProductsBox.SelectedItem is Material material)
  70. {
  71. var materialDuplicate = BasketBox.Items.Cast<ProductMaterial>().ToList().SingleOrDefault(x => x.Material == material);
  72. if (materialDuplicate == null)
  73. {
  74. BasketBox.Items.Add(new ProductMaterial() { Material = material, Count = 1 });
  75. }
  76. else
  77. {
  78. materialDuplicate.Count++;
  79. }
  80. BasketBox.Items.Refresh();
  81. }
  82. }
  83. private void BtnRemove_Click(object sender, RoutedEventArgs e)
  84. {
  85. if (BasketBox.SelectedItem is ProductMaterial productMaterial)
  86. {
  87. if (productMaterial.Count == 1)
  88. {
  89. BasketBox.Items.Remove(productMaterial);
  90. }
  91. else
  92. {
  93. productMaterial.Count--;
  94. }
  95. BasketBox.Items.Refresh();
  96. }
  97. }
  98. private void BtnSubmit_Click(object sender, RoutedEventArgs e)
  99. {
  100. var product = DataContext as Product;
  101. var productMaterials = BasketBox.Items.Cast<ProductMaterial>().ToList();
  102. product.Image = _path;
  103. product.ProductMaterial = productMaterials;
  104. Helper.GetContext().SaveChanges();
  105. Close();
  106. }
  107. private void BtnDelete_Click(object sender, RoutedEventArgs e)
  108. {
  109. Helper.GetContext().Product.Remove(DataContext as Product);
  110. Helper.GetContext().SaveChanges();
  111. Close();
  112. }
  113. }
  114. }