MainWindow.xaml.cs 9.6 KB

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