MainWindow.xaml.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. private void Button_Click(object sender, RoutedEventArgs e)
  63. {
  64. Button clickedButton = (Button)sender;
  65. MessageBox.Show(clickedButton.Content.ToString());
  66. }
  67. public MainWindow()
  68. {
  69. InitializeComponent();
  70. DataContext = this;
  71. Globals.dataProvider = new JSONDataProvider();
  72. WorkerList = Globals.dataProvider.getWorker();
  73. WorkerPosList = Globals.dataProvider.getPos().ToList();
  74. WorkerPosList.Insert(0, new WorkerPos { title = "Должность" });
  75. WorkerAgeList = Globals.dataProvider.getAge().ToList();
  76. WorkerSexList = Globals.dataProvider.getSex().ToList();
  77. WorkerSexList.Insert(0, new WorkerSex { title = "Пол" });
  78. WorkerList = Globals.dataProvider.getWorker();
  79. Style buttonStyle = new Style();
  80. buttonStyle.Setters.Add(
  81. new Setter
  82. {
  83. Property = Control.FontFamilyProperty,
  84. Value = new FontFamily("Verdana")
  85. });
  86. buttonStyle.Setters.Add(
  87. new Setter
  88. {
  89. Property = Control.MarginProperty,
  90. Value = new Thickness(10)
  91. });
  92. buttonStyle.Setters.Add(
  93. new Setter
  94. {
  95. Property = Control.BackgroundProperty,
  96. Value = new SolidColorBrush(Colors.LightCyan)
  97. });
  98. buttonStyle.Setters.Add(
  99. new Setter
  100. {
  101. Property = Control.ForegroundProperty,
  102. Value = new SolidColorBrush(Colors.DarkSalmon)
  103. });
  104. button1.Style = buttonStyle;
  105. button2.Style = buttonStyle;
  106. }
  107. public class JSONDataProvider : LocalDataProvider, IDataProvider
  108. {
  109. private List<Worker> _WorkerList;
  110. public JSONDataProvider()
  111. {
  112. var serializer = new DataContractJsonSerializer(typeof(Worker[]));
  113. using (var sr = new StreamReader("./data.json"))
  114. {
  115. _WorkerList = ((Worker[])serializer.ReadObject(sr.BaseStream)).ToList();
  116. }
  117. }
  118. public IEnumerable<Worker> getWorker()
  119. {
  120. return _WorkerList;
  121. }
  122. }
  123. private bool sortAsc = true;
  124. private void ExitButton_Click(object sender, RoutedEventArgs e)
  125. {
  126. Application.Current.Shutdown();
  127. }
  128. private void PosFilterComboBox_SelectionChanged_1(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
  129. {
  130. selectedPos = (PosFilterComboBox.SelectedItem as WorkerPos).title;
  131. Invalidate();
  132. }
  133. private void AgeFilterComboBox_SelectionChanged_2(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
  134. {
  135. selectedAge = (AgeFilterComboBox.SelectedItem as WorkerAge);
  136. Invalidate();
  137. }
  138. private void SexFilterComboBox_SelectionChanged_3(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
  139. {
  140. selectedSex = (SexFilterComboBox.SelectedItem as WorkerSex).title;
  141. Invalidate();
  142. }
  143. private string searchFilter = "";
  144. private void SearchFilter_KeyUp(object sender, KeyEventArgs e)
  145. {
  146. searchFilter = SearchFilterTextBox.Text;
  147. Invalidate();
  148. }
  149. private void RadioButton_Checked(object sender, RoutedEventArgs e)
  150. {
  151. sortAsc = (sender as RadioButton).Tag.ToString() == "1";
  152. Invalidate();
  153. }
  154. private void button2_Click(object sender, RoutedEventArgs e)
  155. {
  156. }
  157. }
  158. interface IDataProvider
  159. {
  160. IEnumerable<Worker> getWorker();
  161. IEnumerable<WorkerPos> getPos();
  162. IEnumerable<WorkerAge> getAge();
  163. IEnumerable<WorkerSex> getSex();
  164. }
  165. public class LocalDataProvider : IDataProvider
  166. {
  167. public IEnumerable<WorkerPos> getPos()
  168. {
  169. return new WorkerPos[]
  170. {
  171. new WorkerPos()
  172. {
  173. title="Директор"
  174. },
  175. new WorkerPos()
  176. {
  177. title="Продавец"
  178. },
  179. new WorkerPos()
  180. {
  181. title="Механик"
  182. },
  183. new WorkerPos()
  184. {
  185. title="Уборщик"
  186. },
  187. new WorkerPos()
  188. {
  189. title="Стажер"
  190. },
  191. new WorkerPos()
  192. {
  193. title="Оператор"
  194. },
  195. };
  196. }
  197. public IEnumerable<WorkerAge> getAge()
  198. {
  199. return new WorkerAge[]
  200. {
  201. new WorkerAge()
  202. {
  203. title="Все возраста",
  204. AgeFrom=0,
  205. AgeTo=110
  206. },
  207. new WorkerAge()
  208. {
  209. title="Дети",
  210. AgeFrom=1,
  211. AgeTo=6
  212. },
  213. new WorkerAge()
  214. {
  215. title="Подростки",
  216. AgeFrom=7,
  217. AgeTo=21
  218. },
  219. new WorkerAge()
  220. {
  221. title="Молодые",
  222. AgeFrom=22,
  223. AgeTo=50
  224. },
  225. new WorkerAge()
  226. {
  227. title="Старики",
  228. AgeFrom=51,
  229. AgeTo=110
  230. }
  231. };
  232. }
  233. public IEnumerable<WorkerSex> getSex()
  234. {
  235. return new WorkerSex[]
  236. {
  237. new WorkerSex()
  238. {
  239. title="Человек"
  240. },
  241. new WorkerSex()
  242. {
  243. title="Женщина"
  244. }
  245. };
  246. }
  247. public IEnumerable<Worker> getWorker()
  248. {
  249. return new Worker[]{
  250. new Worker{
  251. Sex="Человек",
  252. name="Вася",
  253. Age=25,
  254. Pos="Директор"},
  255. new Worker{
  256. Sex="Человек",
  257. name="Петя",
  258. Age=45,
  259. Pos="Продавец"},
  260. new Worker{
  261. Sex="Человек",
  262. name="Степа",
  263. Age = 30,
  264. Pos="Механик"},
  265. new Worker{
  266. Sex="Человек",
  267. name="Миша",
  268. Age = 100,
  269. Pos="Стажер"},
  270. new Worker{
  271. Sex="Женщина",
  272. name="Лена",
  273. Age = 12,
  274. Pos="Оператор"}
  275. };
  276. }
  277. }
  278. }