123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- using Microsoft.Win32;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Shapes;
- namespace Eight
- {
- /// <summary>
- /// Логика взаимодействия для UpdateProductWindow.xaml
- /// </summary>
- public partial class UpdateProductWindow : Window
- {
- private string _path;
- public UpdateProductWindow(Product product)
- {
- InitializeComponent();
- Load();
- DataContext = product;
- foreach(var productMaterial in product.ProductMaterial)
- {
- BasketBox.Items.Add(productMaterial);
- }
- _path = product.ImagePath;
- Img.Source = GetBitmapImage(_path);
- }
- private BitmapImage GetBitmapImage(string path)
- {
- BitmapImage bitmapImage = new BitmapImage();
- bitmapImage.BeginInit();
- bitmapImage.UriSource = new Uri(path);
- bitmapImage.EndInit();
- return bitmapImage;
- }
- private void Load()
- {
- CmbType.ItemsSource = Helper.GetContext().ProductType.ToList();
- ProductsBox.ItemsSource = Helper.GetContext().Material.ToList();
- }
- private void BtnChoose_Click(object sender, RoutedEventArgs e)
- {
- try
- {
- OpenFileDialog openFileDialog = new OpenFileDialog();
- if (openFileDialog.ShowDialog() == true)
- {
- var fileName = openFileDialog.FileName;
- _path = "\\products\\" + openFileDialog.SafeFileName;
- File.Copy(fileName, Environment.CurrentDirectory + "\\products\\" + openFileDialog.SafeFileName, true);
- Img.Source = GetBitmapImage(Environment.CurrentDirectory + "\\products\\" + openFileDialog.SafeFileName);
- }
- }
- catch (Exception)
- {
- return;
- }
- }
- private void BtnAdd_Click(object sender, RoutedEventArgs e)
- {
- if (ProductsBox.SelectedItem is Material material)
- {
- var materialDuplicate = BasketBox.Items.Cast<ProductMaterial>().ToList().SingleOrDefault(x => x.Material == material);
- if (materialDuplicate == null)
- {
- BasketBox.Items.Add(new ProductMaterial() { Material = material, Count = 1 });
- }
- else
- {
- materialDuplicate.Count++;
- }
- BasketBox.Items.Refresh();
- }
- }
- private void BtnRemove_Click(object sender, RoutedEventArgs e)
- {
- if (BasketBox.SelectedItem is ProductMaterial productMaterial)
- {
- if (productMaterial.Count == 1)
- {
- BasketBox.Items.Remove(productMaterial);
- }
- else
- {
- productMaterial.Count--;
- }
- BasketBox.Items.Refresh();
- }
- }
- private void BtnSubmit_Click(object sender, RoutedEventArgs e)
- {
- var product = DataContext as Product;
- var productMaterials = BasketBox.Items.Cast<ProductMaterial>().ToList();
- product.Image = _path;
- product.ProductMaterial = productMaterials;
- Helper.GetContext().SaveChanges();
- Close();
- }
- private void BtnDelete_Click(object sender, RoutedEventArgs e)
- {
- Helper.GetContext().Product.Remove(DataContext as Product);
- Helper.GetContext().SaveChanges();
- Close();
- }
- }
- }
|