MainWindow.xaml.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. using wpf_listbox.Windows;
  15. namespace wpf_listbox
  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 JSONDataProvider();
  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. Style buttonStyle = new Style();
  74. buttonStyle.Setters.Add(
  75. new Setter
  76. {
  77. Property = Control.FontFamilyProperty,
  78. Value = new FontFamily("Verdana")
  79. });
  80. buttonStyle.Setters.Add(
  81. new Setter
  82. {
  83. Property = Control.MarginProperty,
  84. Value = new Thickness(10)
  85. });
  86. buttonStyle.Setters.Add(
  87. new Setter
  88. {
  89. Property = Control.BackgroundProperty,
  90. Value = new SolidColorBrush(Colors.LightGoldenrodYellow)
  91. });
  92. buttonStyle.Setters.Add(
  93. new Setter
  94. {
  95. Property = Control.ForegroundProperty,
  96. Value = new SolidColorBrush(Colors.DarkGreen)
  97. });
  98. button1.Style = buttonStyle;
  99. button2.Style = buttonStyle;
  100. }
  101. private string currentStyle = "StackStyle";
  102. private void PeopleListBox_MouseDoubleClick(
  103. object sender,
  104. MouseButtonEventArgs e)
  105. {
  106. // в создаваемое окно передаем выбранного котика
  107. var detailWindow = new DetailWindow(
  108. PeopleListBox.SelectedItem as People);
  109. detailWindow.ShowDialog();
  110. }
  111. private void ExitButton_Click(object sender, RoutedEventArgs e)
  112. {
  113. Application.Current.Shutdown();
  114. }
  115. private string searchFilter = "";
  116. private void Button_Click(object sender, RoutedEventArgs e)
  117. {
  118. Button clickedButton = (Button)sender;
  119. MessageBox.Show(clickedButton.Content.ToString());
  120. }
  121. private void SearchFilter_KeyUp(object sender, KeyEventArgs e)
  122. {
  123. searchFilter = SearchFilterTextBox.Text;
  124. Invalidate();
  125. }
  126. private void GenderFilterComboBox_SelectionChanged_1(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
  127. {
  128. selectedGender = (GenderFilterComboBox.SelectedItem as PeopleGender).title;
  129. Invalidate();
  130. }
  131. private void AgeFilterComboBox_SelectionChanged_2(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
  132. {
  133. selectedAge = AgeFilterComboBox.SelectedItem as PeopleAge;
  134. Invalidate();
  135. }
  136. private void PlaceFilterComboBox_SelectionChanged_3(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
  137. {
  138. selectedPlace = (PlaceFilterComboBox.SelectedItem as PeoplePlace).title;
  139. Invalidate();
  140. }
  141. private bool sortAsc = true;
  142. private void RadioButton_Checked(object sender, RoutedEventArgs e)
  143. {
  144. sortAsc = (sender as RadioButton).Tag.ToString() == "1";
  145. Invalidate();
  146. }
  147. }
  148. public class LocalDataProvider : IDataProvider
  149. {
  150. public IEnumerable<PeopleGender> getGender()
  151. {
  152. return new PeopleGender[]
  153. {
  154. new PeopleGender()
  155. {
  156. title="Ж"
  157. },
  158. new PeopleGender()
  159. {
  160. title="М"
  161. }
  162. };
  163. }
  164. public IEnumerable<PeopleAge> getAge()
  165. {
  166. return new PeopleAge[]
  167. {
  168. new PeopleAge()
  169. {
  170. title="Все возраста",
  171. AgeFrom=0,
  172. AgeTo=99
  173. },
  174. new PeopleAge()
  175. {
  176. title="Подростки",
  177. AgeFrom=15,
  178. AgeTo=18
  179. },
  180. new PeopleAge()
  181. {
  182. title="Молодые",
  183. AgeFrom=18,
  184. AgeTo=30
  185. },
  186. new PeopleAge()
  187. {
  188. title="В Возврасте",
  189. AgeFrom=30,
  190. AgeTo=99
  191. }
  192. };
  193. }
  194. public IEnumerable<PeoplePlace> getPlace()
  195. {
  196. return new PeoplePlace[]
  197. {
  198. new PeoplePlace()
  199. {
  200. title="Набережная"
  201. },
  202. new PeoplePlace()
  203. {
  204. title="Йолка"
  205. },
  206. new PeoplePlace()
  207. {
  208. title="Бульвар"
  209. },
  210. new PeoplePlace()
  211. {
  212. title="ЙОТК"
  213. },
  214. new PeoplePlace()
  215. {
  216. title="Планета"
  217. },
  218. new PeoplePlace()
  219. {
  220. title="Ремзавод"
  221. },
  222. new PeoplePlace()
  223. {
  224. title="Медведево"
  225. },
  226. new PeoplePlace()
  227. {
  228. title="Пляж"
  229. },
  230. };
  231. }
  232. public IEnumerable<People> getPeople()
  233. {
  234. return new People[]{
  235. new People
  236. {
  237. Age=19,
  238. Name="Марина",
  239. Gender="Ж",
  240. Price=500,
  241. Place="Набережная"
  242. },
  243. new People
  244. {
  245. Price=9600,
  246. Name="Вика",
  247. Age=21,
  248. Gender="Ж",
  249. Place="Йолка"
  250. },
  251. new People
  252. {
  253. Price=50,
  254. Name="Кристина",
  255. Age=17,
  256. Gender="Ж",
  257. Place="Планета"
  258. },
  259. new People
  260. {
  261. Price=2000,
  262. Name="Анджелика",
  263. Age=16,
  264. Gender="Ж",
  265. Place="Бульвар"
  266. },
  267. new People
  268. {
  269. Price=1500,
  270. Name="Костя",
  271. Age=15,
  272. Gender="М",
  273. Place="Бульвар"
  274. },
  275. new People
  276. {
  277. Price=5000,
  278. Name="Вероника",
  279. Age=25,
  280. Gender="Ж",
  281. Place="Набережная"
  282. },
  283. new People
  284. {
  285. Price=1000,
  286. Name="Савелий",
  287. Age=20,
  288. Gender="М",
  289. Place="Пляж"
  290. },
  291. new People
  292. {
  293. Price=1500,
  294. Name="Александр",
  295. Age=39,
  296. Gender="М",
  297. Place="ЙОТК"
  298. },
  299. new People
  300. {
  301. Price=500,
  302. Name="Евгений",
  303. Age=18,
  304. Gender="М",
  305. Place="Медведево"
  306. },
  307. new People
  308. {
  309. Price=2500,
  310. Name="Давид",
  311. Age=18,
  312. Gender="М",
  313. Place="Ремзавод"
  314. },
  315. };
  316. }
  317. IEnumerable<models.People> IDataProvider.getPeople()
  318. {
  319. throw new NotImplementedException();
  320. }
  321. }
  322. public class JSONDataProvider : LocalDataProvider, IDataProvider
  323. {
  324. private List<People> _PeopleList;
  325. public JSONDataProvider()
  326. {
  327. var serializer = new DataContractJsonSerializer(typeof(People[]));
  328. using (var sr = new StreamReader("./data.json"))
  329. {
  330. _PeopleList = ((People[])serializer.ReadObject(sr.BaseStream)).ToList();
  331. }
  332. }
  333. public IEnumerable<People> getPeople()
  334. {
  335. return _PeopleList;
  336. }
  337. }
  338. }