MainWindow.xaml.cs 9.5 KB

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