MainWindow.xaml.cs 7.9 KB

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