MainWindow.xaml.cs 9.2 KB

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