MainWindow.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. using System;
  2. using System.ComponentModel;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Input;
  6. using wpf_listbox.models;
  7. using System.Threading.Tasks;
  8. using System;
  9. using System.Formats.Asn1;
  10. using System.Globalization;
  11. using System.IO;
  12. using System.Runtime.Serialization.Json;
  13. using System.Windows.Media;
  14. namespace wpf_listbox
  15. {
  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("PeopleList"));
  23. }
  24. public string selectedGender = "Пол";
  25. public PeopleAge? selectedAge = null;
  26. public string selectedPlace = "Место";
  27. private IEnumerable<People> _PeopleList;
  28. public IEnumerable<People> PeopleList
  29. {
  30. get
  31. {
  32. var res = _PeopleList;
  33. // фильтруем по возрасту
  34. res = res
  35. .Where(c => (c.Gender == selectedGender || selectedGender == "Пол"))
  36. .Where(c => (selectedAge == null || (c.Age >= selectedAge.AgeFrom && c.Age < selectedAge.AgeTo)))
  37. .Where(c => (c.Place == selectedPlace || selectedPlace == "Место"));
  38. // если фильтр не пустой, то ищем ВХОЖДЕНИЕ подстроки поиска в кличке без учета регистра
  39. if (searchFilter != "")
  40. res = res.Where(c => c.Name.IndexOf(
  41. searchFilter,
  42. StringComparison.OrdinalIgnoreCase) >= 0 || c.Gender.IndexOf(
  43. searchFilter,
  44. StringComparison.OrdinalIgnoreCase) >= 0 || c.Place.IndexOf(
  45. searchFilter,
  46. StringComparison.OrdinalIgnoreCase) >= 0);
  47. if (sortAsc) res = res.OrderBy(c => c.Price);
  48. else res = res.OrderByDescending(c => c.Price);
  49. if (sortAsc) res = res.OrderBy(c => c.Price);
  50. else res = res.OrderByDescending(c => c.Price);
  51. return res;
  52. }
  53. set
  54. {
  55. _PeopleList = value;
  56. }
  57. }
  58. public List<PeopleGender> PeopleGenderList { get; set; }
  59. public List<PeopleAge> PeopleAgeList { get; set; }
  60. public List<PeoplePlace> PeoplePlaceList { get; set; }
  61. public MainWindow()
  62. {
  63. InitializeComponent();
  64. DataContext = this;
  65. Globals.dataProvider = new JSONDataProvider();
  66. PeopleList = Globals.dataProvider.getPeople();
  67. PeopleGenderList = Globals.dataProvider.getGender().ToList();
  68. PeopleGenderList.Insert(0, new PeopleGender { title = "Пол" });
  69. PeopleAgeList = Globals.dataProvider.getAge().ToList();
  70. PeoplePlaceList = Globals.dataProvider.getPlace().ToList();
  71. PeoplePlaceList.Insert(0, new PeoplePlace { title = "Место" });
  72. List<string> styles = new List<string> { "light", "dark" };
  73. styleBox.SelectionChanged += ThemeChange;
  74. styleBox.ItemsSource = styles;
  75. styleBox.SelectedItem = "dark";
  76. Style buttonStyle = new Style();
  77. buttonStyle.Setters.Add(
  78. new Setter
  79. {
  80. Property = Control.FontFamilyProperty,
  81. Value = new FontFamily("Verdana")
  82. });
  83. buttonStyle.Setters.Add(
  84. new Setter
  85. {
  86. Property = Control.MarginProperty,
  87. Value = new Thickness(10)
  88. });
  89. buttonStyle.Setters.Add(
  90. new Setter
  91. {
  92. Property = Control.BackgroundProperty,
  93. Value = new SolidColorBrush(Colors.LightGoldenrodYellow)
  94. });
  95. buttonStyle.Setters.Add(
  96. new Setter
  97. {
  98. Property = Control.ForegroundProperty,
  99. Value = new SolidColorBrush(Colors.DarkGreen)
  100. });
  101. button1.Style = buttonStyle;
  102. button2.Style = buttonStyle;
  103. }
  104. private void ThemeChange(object sender, SelectionChangedEventArgs e)
  105. {
  106. string style = styleBox.SelectedItem as string;
  107. // определяем путь к файлу ресурсов
  108. var uri = new Uri(style + ".xaml", UriKind.Relative);
  109. // загружаем словарь ресурсов
  110. ResourceDictionary resourceDict =
  111. Application.LoadComponent(uri) as ResourceDictionary;
  112. // очищаем коллекцию ресурсов приложения
  113. Application.Current.Resources.Clear();
  114. // добавляем загруженный словарь ресурсов
  115. Application.Current.Resources.MergedDictionaries.Add(resourceDict);
  116. }
  117. private string currentStyle = "StackStyle";
  118. private void ToggleView_Click(object sender, RoutedEventArgs e)
  119. {
  120. currentStyle = currentStyle == "StackStyle" ? "WrapStyle" : "StackStyle";
  121. var newStyle = (Style)TryFindResource(currentStyle);
  122. if (newStyle != null)
  123. PeopleListBox.Style = newStyle;
  124. }
  125. private void ExitButton_Click(object sender, RoutedEventArgs e)
  126. {
  127. Application.Current.Shutdown();
  128. }
  129. private string searchFilter = "";
  130. private void Button_Click(object sender, RoutedEventArgs e)
  131. {
  132. Button clickedButton = (Button)sender;
  133. MessageBox.Show(clickedButton.Content.ToString());
  134. }
  135. private void SearchFilter_KeyUp(object sender, KeyEventArgs e)
  136. {
  137. searchFilter = SearchFilterTextBox.Text;
  138. Invalidate();
  139. }
  140. private void GenderFilterComboBox_SelectionChanged_1(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
  141. {
  142. selectedGender = (GenderFilterComboBox.SelectedItem as PeopleGender).title;
  143. Invalidate();
  144. }
  145. private void AgeFilterComboBox_SelectionChanged_2(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
  146. {
  147. selectedAge = AgeFilterComboBox.SelectedItem as PeopleAge;
  148. Invalidate();
  149. }
  150. private void PlaceFilterComboBox_SelectionChanged_3(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
  151. {
  152. selectedPlace = (PlaceFilterComboBox.SelectedItem as PeoplePlace).title;
  153. Invalidate();
  154. }
  155. private bool sortAsc = true;
  156. private void RadioButton_Checked(object sender, RoutedEventArgs e)
  157. {
  158. sortAsc = (sender as RadioButton).Tag.ToString() == "1";
  159. Invalidate();
  160. }
  161. }
  162. public class LocalDataProvider : IDataProvider
  163. {
  164. public IEnumerable<PeopleGender> getGender()
  165. {
  166. return new PeopleGender[]
  167. {
  168. new PeopleGender()
  169. {
  170. title="Ж"
  171. },
  172. new PeopleGender()
  173. {
  174. title="М"
  175. }
  176. };
  177. }
  178. public IEnumerable<PeopleAge> getAge()
  179. {
  180. return new PeopleAge[]
  181. {
  182. new PeopleAge()
  183. {
  184. title="Все возраста",
  185. AgeFrom=0,
  186. AgeTo=99
  187. },
  188. new PeopleAge()
  189. {
  190. title="Подростки",
  191. AgeFrom=15,
  192. AgeTo=18
  193. },
  194. new PeopleAge()
  195. {
  196. title="Молодые",
  197. AgeFrom=18,
  198. AgeTo=30
  199. },
  200. new PeopleAge()
  201. {
  202. title="В Возврасте",
  203. AgeFrom=30,
  204. AgeTo=99
  205. }
  206. };
  207. }
  208. public IEnumerable<PeoplePlace> getPlace()
  209. {
  210. return new PeoplePlace[]
  211. {
  212. new PeoplePlace()
  213. {
  214. title="Набережная"
  215. },
  216. new PeoplePlace()
  217. {
  218. title="Йолка"
  219. },
  220. new PeoplePlace()
  221. {
  222. title="Бульвар"
  223. },
  224. new PeoplePlace()
  225. {
  226. title="ЙОТК"
  227. },
  228. new PeoplePlace()
  229. {
  230. title="Планета"
  231. },
  232. new PeoplePlace()
  233. {
  234. title="Ремзавод"
  235. },
  236. new PeoplePlace()
  237. {
  238. title="Медведево"
  239. },
  240. new PeoplePlace()
  241. {
  242. title="Пляж"
  243. },
  244. };
  245. }
  246. public IEnumerable<People> getPeople()
  247. {
  248. return new People[]{
  249. new People
  250. {
  251. Age=19,
  252. Name="Марина",
  253. Gender="Ж",
  254. Price=500,
  255. Place="Набережная"
  256. },
  257. new People
  258. {
  259. Price=9600,
  260. Name="Вика",
  261. Age=21,
  262. Gender="Ж",
  263. Place="Йолка"
  264. },
  265. new People
  266. {
  267. Price=50,
  268. Name="Кристина",
  269. Age=17,
  270. Gender="Ж",
  271. Place="Планета"
  272. },
  273. new People
  274. {
  275. Price=2000,
  276. Name="Анджелика",
  277. Age=16,
  278. Gender="Ж",
  279. Place="Бульвар"
  280. },
  281. new People
  282. {
  283. Price=1500,
  284. Name="Костя",
  285. Age=15,
  286. Gender="М",
  287. Place="Бульвар"
  288. },
  289. new People
  290. {
  291. Price=5000,
  292. Name="Вероника",
  293. Age=25,
  294. Gender="Ж",
  295. Place="Набережная"
  296. },
  297. new People
  298. {
  299. Price=1000,
  300. Name="Савелий",
  301. Age=20,
  302. Gender="М",
  303. Place="Пляж"
  304. },
  305. new People
  306. {
  307. Price=1500,
  308. Name="Александр",
  309. Age=39,
  310. Gender="М",
  311. Place="ЙОТК"
  312. },
  313. new People
  314. {
  315. Price=500,
  316. Name="Евгений",
  317. Age=18,
  318. Gender="М",
  319. Place="Медведево"
  320. },
  321. new People
  322. {
  323. Price=2500,
  324. Name="Давид",
  325. Age=18,
  326. Gender="М",
  327. Place="Ремзавод"
  328. },
  329. };
  330. }
  331. IEnumerable<models.People> IDataProvider.getPeople()
  332. {
  333. throw new NotImplementedException();
  334. }
  335. }
  336. public class JSONDataProvider : LocalDataProvider, IDataProvider
  337. {
  338. private List<People> _PeopleList;
  339. public JSONDataProvider()
  340. {
  341. var serializer = new DataContractJsonSerializer(typeof(People[]));
  342. using (var sr = new StreamReader("./data.json"))
  343. {
  344. _PeopleList = ((People[])serializer.ReadObject(sr.BaseStream)).ToList();
  345. }
  346. }
  347. public IEnumerable<People> getPeople()
  348. {
  349. return _PeopleList;
  350. }
  351. }
  352. }