MainWindow.xaml.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. using System.ComponentModel;
  2. using System.Diagnostics;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Input;
  6. using WpfApp2.model;
  7. using CsvHelper;
  8. namespace WpfApp2
  9. {
  10. /// <summary>
  11. /// Interaction logic for MainWindow.xaml
  12. /// </summary>
  13. public partial class MainWindow : Window, INotifyPropertyChanged
  14. {
  15. public event PropertyChangedEventHandler PropertyChanged;
  16. private void Invalidate()
  17. {
  18. if (PropertyChanged != null)
  19. PropertyChanged(this, new PropertyChangedEventArgs("SparesList"));
  20. }
  21. public string selectedDetail = "Деталь";
  22. public SparesPrice? selectedPrice = null;
  23. public string selectedType = "Тип ТС";
  24. private IEnumerable<Spares> _SparesList;
  25. public IEnumerable<Spares> SparesList
  26. {
  27. get
  28. {
  29. var res = _SparesList;
  30. res = res
  31. .Where(c => (c.detail == selectedDetail || selectedDetail == "Деталь"))
  32. .Where(c => (selectedPrice == null || (c.price >= selectedPrice.PriceFrom && c.price < selectedPrice.PriceTo)))
  33. .Where(c => (c.type == selectedType || selectedType == "Тип ТС"));
  34. if (searchFilter != "")
  35. res = res.Where(c => c.name.IndexOf(
  36. searchFilter,
  37. StringComparison.OrdinalIgnoreCase) >= 0);
  38. if (sortAsc) res = res.OrderBy(c => c.price);
  39. else res = res.OrderByDescending(c => c.price);
  40. return res;
  41. }
  42. set
  43. {
  44. _SparesList = value;
  45. }
  46. }
  47. public List<SparesDetail> SparesDetailList { get; set; }
  48. public List<SparesPrice> SparesPriceList { get; set; }
  49. public List<SparesType> SparesTypeList { get; set; }
  50. public MainWindow()
  51. {
  52. InitializeComponent();
  53. DataContext = this;
  54. Globals.dataProvider = new LocalDataProvider();
  55. SparesList = Globals.dataProvider.getSpares();
  56. SparesDetailList = Globals.dataProvider.getDetail().ToList();
  57. SparesDetailList.Insert(0, new SparesDetail { title = "Деталь" });
  58. SparesPriceList = Globals.dataProvider.getPrice().ToList();
  59. SparesTypeList = Globals.dataProvider.getType().ToList();
  60. SparesTypeList.Insert(0, new SparesType { title = "Тип ТС" });
  61. Globals.dataProvider = new CSVDataProvider();
  62. SparesList = Globals.dataProvider.getSpares();
  63. }
  64. private bool sortAsc = true;
  65. private void RadioButton_Checked(object sender, RoutedEventArgs e)
  66. {
  67. sortAsc = (sender as RadioButton).Tag.ToString() == "1";
  68. Invalidate();
  69. }
  70. private void ExitButton_Click(object sender, RoutedEventArgs e)
  71. {
  72. Application.Current.Shutdown();
  73. }
  74. private void DetailFilterComboBox_SelectionChanged_1(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
  75. {
  76. selectedDetail = (DetailFilterComboBox.SelectedItem as SparesDetail).title;
  77. Invalidate();
  78. }
  79. private void PriceFilterComboBox_SelectionChanged_2(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
  80. {
  81. selectedPrice = PriceFilterComboBox.SelectedItem as SparesPrice;
  82. Invalidate();
  83. }
  84. private void TypeFilterComboBox_SelectionChanged_3(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
  85. {
  86. selectedType = (TypeFilterComboBox.SelectedItem as SparesType).title;
  87. Invalidate();
  88. }
  89. private string searchFilter = "";
  90. private void SearchFilter_KeyUp(object sender, KeyEventArgs e)
  91. {
  92. searchFilter = SearchFilterTextBox.Text;
  93. Invalidate();
  94. }
  95. }
  96. interface IDataProvider
  97. {
  98. IEnumerable<Spares> getSpares();
  99. IEnumerable<SparesDetail> getDetail();
  100. IEnumerable<SparesPrice> getPrice();
  101. IEnumerable<SparesType> getType();
  102. }
  103. public class LocalDataProvider : IDataProvider
  104. {
  105. public IEnumerable<SparesDetail> getDetail()
  106. {
  107. return new SparesDetail[]
  108. {
  109. new SparesDetail()
  110. {
  111. title="Двигатель"
  112. },
  113. new SparesDetail()
  114. {
  115. title="КПП"
  116. },
  117. new SparesDetail()
  118. {
  119. title="Подвеска"
  120. },
  121. new SparesDetail()
  122. {
  123. title="Стекла"
  124. },
  125. new SparesDetail()
  126. {
  127. title="Колеса"
  128. },
  129. };
  130. }
  131. public IEnumerable<SparesPrice> getPrice()
  132. {
  133. return new SparesPrice[]
  134. {
  135. new SparesPrice()
  136. {
  137. title="Все цены",
  138. PriceFrom=0,
  139. PriceTo=99999999
  140. },
  141. new SparesPrice()
  142. {
  143. title="БУ",
  144. PriceFrom=0,
  145. PriceTo=10000
  146. },
  147. new SparesPrice()
  148. {
  149. title="Дешевая",
  150. PriceFrom=10000,
  151. PriceTo=20000
  152. },
  153. new SparesPrice()
  154. {
  155. title="Средняя",
  156. PriceFrom=20000,
  157. PriceTo=50000
  158. },
  159. new SparesPrice()
  160. {
  161. title="Элитная",
  162. PriceFrom=50000,
  163. PriceTo=99999999
  164. }
  165. };
  166. }
  167. public IEnumerable<SparesType> getType()
  168. {
  169. return new SparesType[]
  170. {
  171. new SparesType()
  172. {
  173. title="Легкая"
  174. },
  175. new SparesType()
  176. {
  177. title="Грузовая"
  178. },
  179. };
  180. }
  181. public IEnumerable<Spares> getSpares()
  182. {
  183. return new Spares[]{
  184. new Spares{
  185. type="Легкая",
  186. name="Двигатель МБ140(ОК2281.4)",
  187. price = 72000,
  188. detail="Двигатель"},
  189. new Spares{
  190. type="Легкая",
  191. name="КПП 5-СТ РС-ПДР",
  192. price = 20000,
  193. detail="КПП"},
  194. new Spares{
  195. type="Грузовая",
  196. name="Подвеска ПДРС1337",
  197. price = 15000,
  198. detail="Подвеска"},
  199. new Spares{
  200. type="Легкая",
  201. name="Шина 15-Р",
  202. price = 3000,
  203. detail="Колеса"},
  204. new Spares{
  205. type="Грузовая",
  206. name="Лобовое стекло СТ150",
  207. price = 40000,
  208. detail="Стекла"},
  209. };
  210. }
  211. }
  212. }