MainWindow.xaml.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. namespace wpf_listbox
  14. {
  15. public partial class MainWindow : Window, INotifyPropertyChanged
  16. {
  17. public event PropertyChangedEventHandler PropertyChanged;
  18. private void Invalidate()
  19. {
  20. if (PropertyChanged != null)
  21. PropertyChanged(this, new PropertyChangedEventArgs("PeopleList"));
  22. }
  23. public string selectedGender = "Пол";
  24. public PeopleAge? selectedAge = null;
  25. public string selectedPlace = "Место";
  26. private IEnumerable<People> _PeopleList;
  27. public IEnumerable<People> PeopleList
  28. {
  29. get
  30. {
  31. var res = _PeopleList;
  32. // фильтруем по возрасту
  33. res = res
  34. .Where(c => (c.Gender == selectedGender || selectedGender == "Пол"))
  35. .Where(c => (selectedAge == null || (c.Age >= selectedAge.AgeFrom && c.Age < selectedAge.AgeTo)))
  36. .Where(c => (c.Place == selectedPlace || selectedPlace == "Место"));
  37. // если фильтр не пустой, то ищем ВХОЖДЕНИЕ подстроки поиска в кличке без учета регистра
  38. if (searchFilter != "")
  39. res = res.Where(c => c.Name.IndexOf(
  40. searchFilter,
  41. StringComparison.OrdinalIgnoreCase) >= 0 || c.Gender.IndexOf(
  42. searchFilter,
  43. StringComparison.OrdinalIgnoreCase) >= 0 || c.Place.IndexOf(
  44. searchFilter,
  45. StringComparison.OrdinalIgnoreCase) >= 0);
  46. if (sortAsc) res = res.OrderBy(c => c.Price);
  47. else res = res.OrderByDescending(c => c.Price);
  48. if (sortAsc) res = res.OrderBy(c => c.Price);
  49. else res = res.OrderByDescending(c => c.Price);
  50. return res;
  51. }
  52. set
  53. {
  54. _PeopleList = value;
  55. }
  56. }
  57. public List<PeopleGender> PeopleGenderList { get; set; }
  58. public List<PeopleAge> PeopleAgeList { get; set; }
  59. public List<PeoplePlace> PeoplePlaceList { get; set; }
  60. public MainWindow()
  61. {
  62. InitializeComponent();
  63. DataContext = this;
  64. Globals.dataProvider = new JSONDataProvider();
  65. PeopleList = Globals.dataProvider.getPeople();
  66. PeopleGenderList = Globals.dataProvider.getGender().ToList();
  67. PeopleGenderList.Insert(0, new PeopleGender { title = "Пол" });
  68. PeopleAgeList = Globals.dataProvider.getAge().ToList();
  69. PeoplePlaceList = Globals.dataProvider.getPlace().ToList();
  70. PeoplePlaceList.Insert(0, new PeoplePlace { title = "Место" });
  71. }
  72. private void ExitButton_Click(object sender, RoutedEventArgs e)
  73. {
  74. Application.Current.Shutdown();
  75. }
  76. private string searchFilter = "";
  77. private void SearchFilter_KeyUp(object sender, KeyEventArgs e)
  78. {
  79. searchFilter = SearchFilterTextBox.Text;
  80. Invalidate();
  81. }
  82. private void GenderFilterComboBox_SelectionChanged_1(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
  83. {
  84. selectedGender = (GenderFilterComboBox.SelectedItem as PeopleGender).title;
  85. Invalidate();
  86. }
  87. private void AgeFilterComboBox_SelectionChanged_2(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
  88. {
  89. selectedAge = AgeFilterComboBox.SelectedItem as PeopleAge;
  90. Invalidate();
  91. }
  92. private void PlaceFilterComboBox_SelectionChanged_3(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
  93. {
  94. selectedPlace = (PlaceFilterComboBox.SelectedItem as PeoplePlace).title;
  95. Invalidate();
  96. }
  97. private bool sortAsc = true;
  98. private void RadioButton_Checked(object sender, RoutedEventArgs e)
  99. {
  100. sortAsc = (sender as RadioButton).Tag.ToString() == "1";
  101. Invalidate();
  102. }
  103. }
  104. public class LocalDataProvider : IDataProvider
  105. {
  106. public IEnumerable<PeopleGender> getGender()
  107. {
  108. return new PeopleGender[]
  109. {
  110. new PeopleGender()
  111. {
  112. title="Ж"
  113. },
  114. new PeopleGender()
  115. {
  116. title="М"
  117. }
  118. };
  119. }
  120. public IEnumerable<PeopleAge> getAge()
  121. {
  122. return new PeopleAge[]
  123. {
  124. new PeopleAge()
  125. {
  126. title="Все возраста",
  127. AgeFrom=0,
  128. AgeTo=99
  129. },
  130. new PeopleAge()
  131. {
  132. title="Подростки",
  133. AgeFrom=15,
  134. AgeTo=18
  135. },
  136. new PeopleAge()
  137. {
  138. title="Молодые",
  139. AgeFrom=18,
  140. AgeTo=30
  141. },
  142. new PeopleAge()
  143. {
  144. title="В Возврасте",
  145. AgeFrom=30,
  146. AgeTo=99
  147. }
  148. };
  149. }
  150. public IEnumerable<PeoplePlace> getPlace()
  151. {
  152. return new PeoplePlace[]
  153. {
  154. new PeoplePlace()
  155. {
  156. title="Набережная"
  157. },
  158. new PeoplePlace()
  159. {
  160. title="Йолка"
  161. },
  162. new PeoplePlace()
  163. {
  164. title="Бульвар"
  165. },
  166. new PeoplePlace()
  167. {
  168. title="ЙОТК"
  169. },
  170. new PeoplePlace()
  171. {
  172. title="Планета"
  173. },
  174. new PeoplePlace()
  175. {
  176. title="Ремзавод"
  177. },
  178. new PeoplePlace()
  179. {
  180. title="Медведево"
  181. },
  182. new PeoplePlace()
  183. {
  184. title="Пляж"
  185. },
  186. };
  187. }
  188. public IEnumerable<People> getPeople()
  189. {
  190. return new People[]{
  191. new People
  192. {
  193. Age=19,
  194. Name="Марина",
  195. Gender="Ж",
  196. Price=500,
  197. Place="Набережная"
  198. },
  199. new People
  200. {
  201. Price=9600,
  202. Name="Вика",
  203. Age=21,
  204. Gender="Ж",
  205. Place="Йолка"
  206. },
  207. new People
  208. {
  209. Price=50,
  210. Name="Кристина",
  211. Age=17,
  212. Gender="Ж",
  213. Place="Планета"
  214. },
  215. new People
  216. {
  217. Price=2000,
  218. Name="Анджелика",
  219. Age=16,
  220. Gender="Ж",
  221. Place="Бульвар"
  222. },
  223. new People
  224. {
  225. Price=1500,
  226. Name="Костя",
  227. Age=15,
  228. Gender="М",
  229. Place="Бульвар"
  230. },
  231. new People
  232. {
  233. Price=5000,
  234. Name="Вероника",
  235. Age=25,
  236. Gender="Ж",
  237. Place="Набережная"
  238. },
  239. new People
  240. {
  241. Price=1000,
  242. Name="Савелий",
  243. Age=20,
  244. Gender="М",
  245. Place="Пляж"
  246. },
  247. new People
  248. {
  249. Price=1500,
  250. Name="Александр",
  251. Age=39,
  252. Gender="М",
  253. Place="ЙОТК"
  254. },
  255. new People
  256. {
  257. Price=500,
  258. Name="Евгений",
  259. Age=18,
  260. Gender="М",
  261. Place="Медведево"
  262. },
  263. new People
  264. {
  265. Price=2500,
  266. Name="Давид",
  267. Age=18,
  268. Gender="М",
  269. Place="Ремзавод"
  270. },
  271. };
  272. }
  273. IEnumerable<models.People> IDataProvider.getPeople()
  274. {
  275. throw new NotImplementedException();
  276. }
  277. }
  278. public class JSONDataProvider : LocalDataProvider, IDataProvider
  279. {
  280. private List<People> _PeopleList;
  281. public JSONDataProvider()
  282. {
  283. var serializer = new DataContractJsonSerializer(typeof(People[]));
  284. using (var sr = new StreamReader("./data.json"))
  285. {
  286. _PeopleList = ((People[])serializer.ReadObject(sr.BaseStream)).ToList();
  287. }
  288. }
  289. public IEnumerable<People> getPeople()
  290. {
  291. return _PeopleList;
  292. }
  293. }
  294. }