MainWindow.xaml.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 WpfApp1.model;
  13. using System.Threading.Tasks;
  14. using System;
  15. using WpfApp1.model;
  16. using WpfApp1.model;
  17. using WpfApp1.model;
  18. using System.IO;
  19. using System.Runtime.Serialization.Json;
  20. namespace WpfApp1
  21. {
  22. /// <summary>
  23. /// Interaction logic for MainWindow.xaml
  24. /// </summary>
  25. public partial class MainWindow : Window, INotifyPropertyChanged
  26. {
  27. public event PropertyChangedEventHandler PropertyChanged;
  28. private void Invalidate()
  29. {
  30. if (PropertyChanged != null)
  31. PropertyChanged(this, new PropertyChangedEventArgs("WorkerList"));
  32. }
  33. public string selectedPos = "Должность";
  34. public WorkerAge? selectedAge = null;
  35. public string selectedSex = "Пол";
  36. private IEnumerable<Worker> _WorkerList;
  37. public IEnumerable<Worker> WorkerList
  38. {
  39. get
  40. {
  41. var res = _WorkerList;
  42. res = res
  43. .Where(c => (c.Pos == selectedPos || selectedPos == "Должность"))
  44. .Where(c => (selectedAge == null || (c.Age >= selectedAge.AgeFrom && c.Age < selectedAge.AgeTo)))
  45. .Where(c => (c.Sex == selectedSex || selectedSex == "Пол"));
  46. if (searchFilter != "")
  47. res = res.Where(c => c.name.IndexOf(
  48. searchFilter,
  49. StringComparison.OrdinalIgnoreCase) >= 0);
  50. if (sortAsc) res = res.OrderBy(c => c.Age);
  51. else res = res.OrderByDescending(c => c.Age);
  52. return res;
  53. }
  54. set
  55. {
  56. _WorkerList = value;
  57. }
  58. }
  59. public List<WorkerPos> WorkerPosList { get; set; }
  60. public List<WorkerAge> WorkerAgeList { get; set; }
  61. public List<WorkerSex> WorkerSexList { get; set; }
  62. public MainWindow()
  63. {
  64. InitializeComponent();
  65. DataContext = this;
  66. Globals.dataProvider = new JSONDataProvider();
  67. WorkerList = Globals.dataProvider.getWorker();
  68. WorkerPosList = Globals.dataProvider.getPos().ToList();
  69. WorkerPosList.Insert(0, new WorkerPos { title = "Должность" });
  70. WorkerAgeList = Globals.dataProvider.getAge().ToList();
  71. WorkerSexList = Globals.dataProvider.getSex().ToList();
  72. WorkerSexList.Insert(0, new WorkerSex { title = "Пол" });
  73. WorkerList = Globals.dataProvider.getWorker();
  74. }
  75. public class JSONDataProvider : LocalDataProvider, IDataProvider
  76. {
  77. private List<Worker> _WorkerList;
  78. public JSONDataProvider()
  79. {
  80. var serializer = new DataContractJsonSerializer(typeof(Worker[]));
  81. using (var sr = new StreamReader("./data.json"))
  82. {
  83. _WorkerList = ((Worker[])serializer.ReadObject(sr.BaseStream)).ToList();
  84. }
  85. }
  86. public IEnumerable<Worker> getWorker()
  87. {
  88. return _WorkerList;
  89. }
  90. }
  91. private bool sortAsc = true;
  92. private void ExitButton_Click(object sender, RoutedEventArgs e)
  93. {
  94. Application.Current.Shutdown();
  95. }
  96. private void PosFilterComboBox_SelectionChanged_1(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
  97. {
  98. selectedPos = (PosFilterComboBox.SelectedItem as WorkerPos).title;
  99. Invalidate();
  100. }
  101. private void AgeFilterComboBox_SelectionChanged_2(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
  102. {
  103. selectedAge = (AgeFilterComboBox.SelectedItem as WorkerAge);
  104. Invalidate();
  105. }
  106. private void SexFilterComboBox_SelectionChanged_3(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
  107. {
  108. selectedSex = (SexFilterComboBox.SelectedItem as WorkerSex).title;
  109. Invalidate();
  110. }
  111. private string searchFilter = "";
  112. private void SearchFilter_KeyUp(object sender, KeyEventArgs e)
  113. {
  114. searchFilter = SearchFilterTextBox.Text;
  115. Invalidate();
  116. }
  117. private void RadioButton_Checked(object sender, RoutedEventArgs e)
  118. {
  119. sortAsc = (sender as RadioButton).Tag.ToString() == "1";
  120. Invalidate();
  121. }
  122. }
  123. interface IDataProvider
  124. {
  125. IEnumerable<Worker> getWorker();
  126. IEnumerable<WorkerPos> getPos();
  127. IEnumerable<WorkerAge> getAge();
  128. IEnumerable<WorkerSex> getSex();
  129. }
  130. public class LocalDataProvider : IDataProvider
  131. {
  132. public IEnumerable<WorkerPos> getPos()
  133. {
  134. return new WorkerPos[]
  135. {
  136. new WorkerPos()
  137. {
  138. title="Директор"
  139. },
  140. new WorkerPos()
  141. {
  142. title="Продавец"
  143. },
  144. new WorkerPos()
  145. {
  146. title="Механик"
  147. },
  148. new WorkerPos()
  149. {
  150. title="Уборщик"
  151. },
  152. new WorkerPos()
  153. {
  154. title="Стажер"
  155. },
  156. new WorkerPos()
  157. {
  158. title="Оператор"
  159. },
  160. };
  161. }
  162. public IEnumerable<WorkerAge> getAge()
  163. {
  164. return new WorkerAge[]
  165. {
  166. new WorkerAge()
  167. {
  168. title="Все возраста",
  169. AgeFrom=0,
  170. AgeTo=110
  171. },
  172. new WorkerAge()
  173. {
  174. title="Дети",
  175. AgeFrom=1,
  176. AgeTo=6
  177. },
  178. new WorkerAge()
  179. {
  180. title="Подростки",
  181. AgeFrom=7,
  182. AgeTo=21
  183. },
  184. new WorkerAge()
  185. {
  186. title="Молодые",
  187. AgeFrom=22,
  188. AgeTo=50
  189. },
  190. new WorkerAge()
  191. {
  192. title="Старики",
  193. AgeFrom=51,
  194. AgeTo=110
  195. }
  196. };
  197. }
  198. public IEnumerable<WorkerSex> getSex()
  199. {
  200. return new WorkerSex[]
  201. {
  202. new WorkerSex()
  203. {
  204. title="Человек"
  205. },
  206. new WorkerSex()
  207. {
  208. title="Женщина"
  209. }
  210. };
  211. }
  212. public IEnumerable<Worker> getWorker()
  213. {
  214. return new Worker[]{
  215. new Worker{
  216. Sex="Человек",
  217. name="Вася",
  218. Age=25,
  219. Pos="Директор"},
  220. new Worker{
  221. Sex="Человек",
  222. name="Петя",
  223. Age=45,
  224. Pos="Продавец"},
  225. new Worker{
  226. Sex="Человек",
  227. name="Степа",
  228. Age = 30,
  229. Pos="Механик"},
  230. new Worker{
  231. Sex="Человек",
  232. name="Миша",
  233. Age = 100,
  234. Pos="Стажер"},
  235. new Worker{
  236. Sex="Женщина",
  237. name="Лена",
  238. Age = 12,
  239. Pos="Оператор"}
  240. };
  241. }
  242. }
  243. }