MainWindow.xaml.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. private void carListBox_MouseDoubleClick(
  130. object sender,
  131. MouseButtonEventArgs e)
  132. {
  133. Window1 detailWindow = new Window1(
  134. carListBox.SelectedItem as cars);
  135. detailWindow.ShowDialog();
  136. }
  137. }
  138. interface IDataProvider
  139. {
  140. IEnumerable<cars> getCars();
  141. IEnumerable<Namecar> getName();
  142. IEnumerable<Carsprice> getPrice();
  143. }
  144. public class LocalDataProvider : IDataProvider
  145. {
  146. public IEnumerable<Namecar> getName()
  147. {
  148. return new Namecar[]
  149. {
  150. new Namecar()
  151. {
  152. title="Toyota Trueno AE86"
  153. },
  154. new Namecar()
  155. {
  156. title="Toyota Supra A80"
  157. },
  158. new Namecar()
  159. {
  160. title="Nissal Skyline R34"
  161. },
  162. new Namecar()
  163. {
  164. title="Nissan Silvia S15"
  165. },
  166. new Namecar()
  167. {
  168. title="Toyota Camry 3.5"
  169. },
  170. new Namecar()
  171. {
  172. title="Audi RS 6"
  173. },
  174. new Namecar()
  175. {
  176. title="Трактор LOVOL TE354 HT"
  177. },
  178. new Namecar()
  179. {
  180. title="BMW M5 F90"
  181. },
  182. new Namecar()
  183. {
  184. title="BMW E36"
  185. },
  186. new Namecar()
  187. {
  188. title="Daewoo Matiz"
  189. }
  190. };
  191. }
  192. public IEnumerable<Carsprice> getPrice()
  193. {
  194. return new Carsprice[]
  195. {
  196. new Carsprice()
  197. {
  198. title="Все цены",
  199. priceFrom=0,
  200. priceTo=70000
  201. },
  202. new Carsprice()
  203. {
  204. title="Малая цена",
  205. priceFrom=0,
  206. priceTo=15001
  207. },
  208. new Carsprice()
  209. {
  210. title="Средняя цена",
  211. priceFrom=20000,
  212. priceTo=35000
  213. },
  214. new Carsprice()
  215. {
  216. title="Высокая цена",
  217. priceFrom=35001,
  218. priceTo=70000
  219. }
  220. };
  221. }
  222. public IEnumerable<cars> getCars()
  223. {
  224. return new cars[] {
  225. new cars {
  226. price = 15000,
  227. name = "Toyota Trueno AE86",
  228. year = 1995,
  229. },
  230. new cars {
  231. price = 30000,
  232. name = "Toyota Supra A80",
  233. year = 1996,
  234. },
  235. new cars {
  236. price = 25000,
  237. name = "Nissal Skyline R34",
  238. year = 1996
  239. },
  240. new cars {
  241. price = 20000,
  242. name = "Nissan Silvia S15",
  243. year = 1999,
  244. },
  245. new cars {
  246. price = 20000,
  247. name = "Toyota Camry 3.5",
  248. year = 2020,
  249. },
  250. new cars {
  251. price = 30000,
  252. name = "Audi RS 6",
  253. year = 2016,
  254. },
  255. new cars {
  256. price = 66666,
  257. name = "Трактор LOVOL TE354 HT",
  258. year = 2024,
  259. },
  260. new cars {
  261. price = 30000,
  262. name = "BMW M5 F90",
  263. year = 2019,
  264. },
  265. new cars {
  266. price = 35000,
  267. name = "BMW E36",
  268. year = 2006,
  269. },
  270. new cars {
  271. price = 20000,
  272. name = "Daewoo Matiz",
  273. year = 2010,
  274. },
  275. };
  276. }
  277. }
  278. }