0
0

MainWindow.xaml.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. using System.ComponentModel;
  2. using System.Security.Cryptography.X509Certificates;
  3. using System.Text;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Data;
  7. using System.Windows.Documents;
  8. using System.Windows.Input;
  9. using System.Windows.Media;
  10. using System.Windows.Media.Imaging;
  11. using System.Windows.Navigation;
  12. using System.Windows.Shapes;
  13. using Dapper;
  14. using MySqlConnector;
  15. namespace wpf_connection3
  16. {
  17. public partial class MainWindow : Window,
  18. INotifyPropertyChanged
  19. {
  20. private int _currentPage = 1;
  21. public int currentPage
  22. {
  23. get
  24. {
  25. return _currentPage;
  26. }
  27. set
  28. {
  29. _currentPage = value;
  30. Invalidate();
  31. }
  32. }
  33. public List<string> pageList { get; set; } = new List<string>();
  34. private int productCount;
  35. public IEnumerable<Product> productList
  36. {
  37. get
  38. {
  39. switch (sortType)
  40. {
  41. case 0:
  42. Globals.dataProvider.setOrder("");
  43. break;
  44. case 1:
  45. Globals.dataProvider.setOrder("Title");
  46. break;
  47. case 2:
  48. Globals.dataProvider.setOrder("Title DESC");
  49. break;
  50. case 3:
  51. Globals.dataProvider.setOrder("ProductionWorkshopNumber");
  52. break;
  53. case 4:
  54. Globals.dataProvider.setOrder("ProductionWorkshopNumber DESC");
  55. break;
  56. case 5:
  57. Globals.dataProvider.setOrder("MaterialCost");
  58. break;
  59. case 6:
  60. Globals.dataProvider.setOrder("MaterialCost DESC");
  61. break;
  62. }
  63. Globals.dataProvider.clearFilter();
  64. if (productTypeFilterId > 0)
  65. Globals.dataProvider.addFilter(
  66. "ProductTypeID = @ProductTypeID",
  67. new { ProductTypeID = productTypeFilterId }
  68. );
  69. if (searchFilter.Length > 0)
  70. {
  71. Globals.dataProvider.addFilter(
  72. //"(Title LIKE @search OR Description LIKE @search)",
  73. "(Title LIKE @search)",
  74. new { search = $"%{searchFilter}%" }
  75. );
  76. }
  77. var result = Globals.dataProvider.getProduct(currentPage);
  78. productCount = Globals.dataProvider.getProductCount();
  79. pageList.Clear();
  80. pageList.Add("<");
  81. for (int i = 1; i < (productCount / Globals.PAGE_LEN) + 1; i++)
  82. {
  83. pageList.Add(i.ToString());
  84. }
  85. pageList.Add(">");
  86. // PageListListBox.ItemsSource = pageList;
  87. Invalidate("pageList");
  88. return result;
  89. }
  90. }
  91. private void ExitButton_Click(object sender, RoutedEventArgs e)
  92. {
  93. Application.Current.Shutdown();
  94. }
  95. public MainWindow()
  96. {
  97. InitializeComponent();
  98. DataContext = this;
  99. Globals.dataProvider = new DBDataProvider();
  100. productTypeList = Globals.dataProvider.getProductTypes().ToList();
  101. productTypeList.Insert(0, new ProductType { Title = "Все типы продукции" });
  102. }
  103. public event PropertyChangedEventHandler? PropertyChanged;
  104. private void Invalidate(string propertyName = "productList")
  105. {
  106. if (PropertyChanged != null)
  107. PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  108. }
  109. private void TextBlock_PreviewMouseDown(object sender, MouseButtonEventArgs e)
  110. {
  111. switch ((sender as TextBlock).Text)
  112. {
  113. case "<":
  114. if (currentPage > 1) currentPage--;
  115. return;
  116. case ">":
  117. if (currentPage < productCount / Globals.PAGE_LEN) currentPage++;
  118. return;
  119. default:
  120. currentPage = Convert.ToInt32(
  121. (sender as TextBlock).Text);
  122. return;
  123. }
  124. }
  125. public string[] sortList { get; set; } =
  126. {
  127. "Без сортировки",
  128. "название по убыванию",
  129. "название по возрастанию",
  130. "номер цеха по убыванию",
  131. "номер цеха по возрастанию",
  132. "цена по убыванию",
  133. "цена по возрастанию"
  134. };
  135. private int sortType = 0;
  136. private void SortTypeComboBox_SelectionChanged(
  137. object? sender,
  138. SelectionChangedEventArgs e)
  139. {
  140. if (SortTypeComboBox != null)
  141. {
  142. sortType = SortTypeComboBox.SelectedIndex;
  143. Invalidate();
  144. }
  145. }
  146. public List<ProductType> productTypeList { get; set; }
  147. private int productTypeFilterId = 0;
  148. private void ProductTypeFilter_SelectionChanged(object sender, SelectionChangedEventArgs e)
  149. {
  150. // запоминаем ID выбранного типа
  151. productTypeFilterId = (ProductTypeFilter.SelectedItem as ProductType).ID;
  152. Invalidate();
  153. }
  154. private string searchFilter = "";
  155. private void searchFilterTextBox_KeyUp(object sender, KeyEventArgs e)
  156. {
  157. if (searchFilterTextBox.Text != null && searchFilterTextBox.Text != "")
  158. {
  159. searchFilter = searchFilterTextBox.Text;
  160. Invalidate();
  161. }
  162. }
  163. }
  164. }