MainWindow.xaml.cs 9.6 KB

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