AddMaterial.xaml.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using sql_pagining.models;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  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.Shapes;
  15. namespace sql_pagining.Windows
  16. {
  17. public partial class AddMaterial : Window
  18. {
  19. private ProductMaterial _productMaterial;
  20. public List<Material> allMaterials { get; }
  21. public AddMaterial(ProductMaterial productMaterial)
  22. {
  23. InitializeComponent();
  24. DataContext = this;
  25. _productMaterial = productMaterial;
  26. allMaterials = new List<Material>(Globals.dataProvider.getAvailableMaterials().ToList());
  27. MaterialComboBox.ItemsSource = allMaterials;
  28. MaterialComboBox.DisplayMemberPath = "Title";
  29. MaterialComboBox.SelectedValuePath = "ID";
  30. }
  31. private void SearchTextBox_TextChanged(object sender, TextChangedEventArgs e)
  32. {
  33. string searchText = SearchTextBox.Text.ToLower();
  34. var filteredMaterials = allMaterials
  35. .Where(m => m.Title.ToLower().Contains(searchText))
  36. .ToList();
  37. MaterialComboBox.ItemsSource = filteredMaterials;
  38. MaterialComboBox.IsDropDownOpen = true;
  39. }
  40. private void AddMaterialButton_Click(object sender, RoutedEventArgs e)
  41. {
  42. try
  43. {
  44. if (MaterialComboBox.SelectedItem == null)
  45. {
  46. throw new Exception("Необходимо выбрать материал.");
  47. }
  48. if (!int.TryParse(MaterialCountTextBox.Text, out int count) || count <= 0)
  49. {
  50. throw new Exception("Введите корректное количество.");
  51. }
  52. _productMaterial.MaterialID = (int)MaterialComboBox.SelectedValue;
  53. _productMaterial.Count = count;
  54. Globals.dataProvider.addProductMaterial(_productMaterial);
  55. DialogResult = true;
  56. }
  57. catch (Exception ex)
  58. {
  59. MessageBox.Show(ex.Message, "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
  60. }
  61. }
  62. }
  63. }