MainWindow.xaml.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. using System;
  2. using System.ComponentModel;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Input;
  6. using System.Windows.Media;
  7. using WpfApp1.model;
  8. namespace WpfApp1
  9. {
  10. /// <summary>
  11. /// Interaction logic for MainWindow.xaml
  12. /// </summary>
  13. public partial class MainWindow : Window, INotifyPropertyChanged
  14. {
  15. private string searchFilter = "";
  16. private void SearchFilter_KeyUp(object sender, KeyEventArgs e)
  17. {
  18. searchFilter = SearchFilterTextBox.Text;
  19. Invalidate();
  20. }
  21. private bool sortAsc = true;
  22. private void RadioButton_Checked(object sender, RoutedEventArgs e)
  23. {
  24. sortAsc = (sender as RadioButton).Tag.ToString() == "1";
  25. Invalidate();
  26. }
  27. public event PropertyChangedEventHandler PropertyChanged;
  28. private void Invalidate()
  29. {
  30. if (PropertyChanged != null)
  31. PropertyChanged(this, new PropertyChangedEventArgs("CarList"));
  32. }
  33. public string selectedname = "Все автомобили";
  34. public Carsprice? selectedprice = null;
  35. private IEnumerable<cars> _CarList;
  36. public IEnumerable<cars> CarList
  37. {
  38. get
  39. {
  40. var res = _CarList;
  41. res = res
  42. .Where(c => (c.name == selectedname || selectedname == "Все автомобили"))
  43. .Where(c => (selectedprice == null || (c.price >= selectedprice.priceFrom && c.price < selectedprice.priceTo)));
  44. if (searchFilter != "")
  45. res = res.Where(c => c.name.IndexOf(
  46. searchFilter,
  47. StringComparison.OrdinalIgnoreCase) >= 0);
  48. if (sortAsc) res = res.OrderBy(c => c.year);
  49. else res = res.OrderByDescending(c => c.year);
  50. return res;
  51. }
  52. set
  53. {
  54. _CarList = value;
  55. }
  56. }
  57. public List<cars> Cars { get; set; }
  58. public List<Namecar> CarinfoList { get; set; }
  59. public List<Carsprice> CarpriceList { get; set; }
  60. public MainWindow()
  61. {
  62. InitializeComponent();
  63. Style buttonStyle = new Style();
  64. buttonStyle.Setters.Add(
  65. new Setter
  66. {
  67. Property = Control.FontFamilyProperty,
  68. Value = new FontFamily("Verdana")
  69. });
  70. buttonStyle.Setters.Add(
  71. new Setter
  72. {
  73. Property = Control.MarginProperty,
  74. Value = new Thickness(10)
  75. });
  76. buttonStyle.Setters.Add(
  77. new Setter
  78. {
  79. Property = Control.BackgroundProperty,
  80. Value = new SolidColorBrush(Colors.Black)
  81. });
  82. buttonStyle.Setters.Add(
  83. new Setter
  84. {
  85. Property = Control.ForegroundProperty,
  86. Value = new SolidColorBrush(Colors.White)
  87. });
  88. buttonStyle.Setters.Add(
  89. new EventSetter
  90. {
  91. Event = Button.ClickEvent,
  92. Handler = new RoutedEventHandler(Button_Click)
  93. });
  94. ExitButton.Style = buttonStyle;
  95. DataContext = this;
  96. Globals.dataProvider = new JSONDataProvider();
  97. CarList = Globals.dataProvider.getCars();
  98. CarinfoList = Globals.dataProvider.getName().ToList();
  99. CarinfoList.Insert(0, new Namecar { title = "Все автомобили" });
  100. CarpriceList = Globals.dataProvider.getPrice().ToList();
  101. }
  102. private string currentStyle = "StackStyle";
  103. private void ToggleView_Click(object sender, RoutedEventArgs e)
  104. {
  105. currentStyle = currentStyle == "StackStyle" ? "WrapStyle" : "StackStyle";
  106. var newStyle = (Style)TryFindResource(currentStyle);
  107. if (newStyle != null)
  108. carListBox.Style = newStyle;
  109. }
  110. private void Button_Click(object sender, RoutedEventArgs e)
  111. {
  112. Button clickedButton = (Button)sender;
  113. MessageBox.Show(clickedButton.Content.ToString());
  114. }
  115. private void ExitButton_Click(object sender, RoutedEventArgs e)
  116. {
  117. Application.Current.Shutdown();
  118. }
  119. private void carnameFilterComboBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
  120. {
  121. selectedname = (carnameFilterComboBox.SelectedItem as Namecar).title;
  122. Invalidate();
  123. }
  124. private void carpriceFilterComboBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
  125. {
  126. selectedprice = carpriceFilterComboBox.SelectedItem as Carsprice;
  127. Invalidate();
  128. }
  129. }
  130. interface IDataProvider
  131. {
  132. IEnumerable<cars> getCars();
  133. IEnumerable<Namecar> getName();
  134. IEnumerable<Carsprice> getPrice();
  135. }
  136. public class LocalDataProvider : IDataProvider
  137. {
  138. public IEnumerable<Namecar> getName()
  139. {
  140. return new Namecar[]
  141. {
  142. new Namecar()
  143. {
  144. title="Toyota Trueno AE86"
  145. },
  146. new Namecar()
  147. {
  148. title="Toyota Supra A80"
  149. },
  150. new Namecar()
  151. {
  152. title="Nissal Skyline R34"
  153. },
  154. new Namecar()
  155. {
  156. title="Nissan Silvia S15"
  157. },
  158. new Namecar()
  159. {
  160. title="Toyota Camry 3.5"
  161. },
  162. new Namecar()
  163. {
  164. title="Audi RS 6"
  165. },
  166. new Namecar()
  167. {
  168. title="Трактор LOVOL TE354 HT"
  169. },
  170. new Namecar()
  171. {
  172. title="BMW M5 F90"
  173. },
  174. new Namecar()
  175. {
  176. title="BMW E36"
  177. },
  178. new Namecar()
  179. {
  180. title="Daewoo Matiz"
  181. }
  182. };
  183. }
  184. public IEnumerable<Carsprice> getPrice()
  185. {
  186. return new Carsprice[]
  187. {
  188. new Carsprice()
  189. {
  190. title="Все цены",
  191. priceFrom=0,
  192. priceTo=70000
  193. },
  194. new Carsprice()
  195. {
  196. title="Малая цена",
  197. priceFrom=0,
  198. priceTo=15001
  199. },
  200. new Carsprice()
  201. {
  202. title="Средняя цена",
  203. priceFrom=20000,
  204. priceTo=35000
  205. },
  206. new Carsprice()
  207. {
  208. title="Высокая цена",
  209. priceFrom=35001,
  210. priceTo=70000
  211. }
  212. };
  213. }
  214. public IEnumerable<cars> getCars()
  215. {
  216. return new cars[] {
  217. new cars {
  218. price = 15000,
  219. name = "Toyota Trueno AE86",
  220. year = 1995,
  221. },
  222. new cars {
  223. price = 30000,
  224. name = "Toyota Supra A80",
  225. year = 1996,
  226. },
  227. new cars {
  228. price = 25000,
  229. name = "Nissal Skyline R34",
  230. year = 1996
  231. },
  232. new cars {
  233. price = 20000,
  234. name = "Nissan Silvia S15",
  235. year = 1999,
  236. },
  237. new cars {
  238. price = 20000,
  239. name = "Toyota Camry 3.5",
  240. year = 2020,
  241. },
  242. new cars {
  243. price = 30000,
  244. name = "Audi RS 6",
  245. year = 2016,
  246. },
  247. new cars {
  248. price = 66666,
  249. name = "Трактор LOVOL TE354 HT",
  250. year = 2024,
  251. },
  252. new cars {
  253. price = 30000,
  254. name = "BMW M5 F90",
  255. year = 2019,
  256. },
  257. new cars {
  258. price = 35000,
  259. name = "BMW E36",
  260. year = 2006,
  261. },
  262. new cars {
  263. price = 20000,
  264. name = "Daewoo Matiz",
  265. year = 2010,
  266. },
  267. };
  268. }
  269. }
  270. }