MainWindow.xaml.cs 9.0 KB

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