ViewChildren.xaml.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Controls.Primitives;
  9. using System.Windows.Data;
  10. using System.Windows.Documents;
  11. using System.Windows.Input;
  12. using System.Windows.Markup;
  13. using System.Windows.Media;
  14. using System.Windows.Media.Imaging;
  15. using System.Windows.Navigation;
  16. using System.Windows.Shapes;
  17. namespace Детский_сад
  18. {
  19. /// <summary>
  20. /// Логика взаимодействия для ViewChildren.xaml
  21. /// </summary>
  22. public partial class ViewChildren : Page
  23. {
  24. Pagination Pagin = new Pagination();
  25. List<Children> ChildList = new List<Children>();
  26. bool StartForFilter = true;
  27. bool StartForSort = true;
  28. public ViewChildren()
  29. {
  30. InitializeComponent();
  31. ChildList = Base.KE.Children.ToList();
  32. lv.ItemsSource = ChildList;
  33. Pagin.CountPage = Base.KE.Children.ToList().Count;
  34. DataContext = Pagin;
  35. CbFilter.SelectedIndex = 0;
  36. CbSort.SelectedIndex = 0;
  37. }
  38. private void btnBack_Click(object sender, RoutedEventArgs e)
  39. {
  40. Base.mainFrame.Navigate(new Account());
  41. }
  42. private void tbMather_Loaded(object sender, RoutedEventArgs e)
  43. {
  44. TextBlock tb = (TextBlock)sender;
  45. int id = Convert.ToInt32(tb.Uid);
  46. Kinships kinships = Base.KE.Kinships.FirstOrDefault(x => x.Id_child == id && x.Parents.Id_gender == 2);
  47. if (kinships != null)
  48. {
  49. tb.Text = "Мать: ";
  50. tb.Text += kinships.Parents.FullName;
  51. }
  52. else
  53. {
  54. tb.Visibility = Visibility.Collapsed;
  55. }
  56. }
  57. private void tbVather_Loaded(object sender, RoutedEventArgs e)
  58. {
  59. TextBlock tb = (TextBlock)sender;
  60. int id = Convert.ToInt32(tb.Uid);
  61. Kinships kinships = Base.KE.Kinships.FirstOrDefault(x => x.Id_child == id && x.Parents.Id_gender == 1);
  62. if (kinships != null)
  63. {
  64. tb.Text = "Отец: ";
  65. tb.Text += kinships.Parents.FullName;
  66. }
  67. else
  68. {
  69. tb.Visibility = Visibility.Collapsed;
  70. }
  71. }
  72. private void btnAdd_Click(object sender, RoutedEventArgs e)
  73. {
  74. Base.mainFrame.Navigate(new AddChild());
  75. }
  76. private void btnDelete_Click(object sender, RoutedEventArgs e)
  77. {
  78. Button button = (Button)sender;
  79. int id = Convert.ToInt32(button.Uid);
  80. Children child = Base.KE.Children.FirstOrDefault(x => x.Id_child == id);
  81. List<Kinships> kinships = Base.KE.Kinships.Where(x => x.Id_child == id).ToList();
  82. Base.KE.Children.Remove(child);
  83. Base.KE.SaveChanges();
  84. foreach (Kinships item in kinships)
  85. {
  86. if (Base.KE.Kinships.Where(x => x.Id_parent == item.Id_parent).ToList().Count == 0)
  87. {
  88. Parents parent = Base.KE.Parents.FirstOrDefault(x => x.Id_parent == item.Id_parent);
  89. Base.KE.Parents.Remove(parent);
  90. }
  91. }
  92. Base.KE.SaveChanges();
  93. MessageBox.Show("Ребёнок успешно удалён", "Дети", MessageBoxButton.OK, MessageBoxImage.Information);
  94. }
  95. private void lv_MouseDoubleClick(object sender, MouseButtonEventArgs e)
  96. {
  97. Children child = (Children)lv.SelectedItem;
  98. Base.mainFrame.Navigate(new AddChild(Base.KE.Children.FirstOrDefault(x => x.Id_child == child.Id_child)));
  99. }
  100. private void CbFilter_SelectionChanged(object sender, SelectionChangedEventArgs e)
  101. {
  102. Filter();
  103. }
  104. private void TBoxFind_TextChanged(object sender, TextChangedEventArgs e)
  105. {
  106. Filter();
  107. }
  108. private void ChBPhoto_Click(object sender, RoutedEventArgs e)
  109. {
  110. Filter();
  111. }
  112. private void Filter()
  113. {
  114. ChildList.Clear();
  115. switch (CbFilter.SelectedIndex)
  116. {
  117. case 1:
  118. ChildList = Base.KE.Children.Where(x => x.Surname.StartsWith(TBoxFind.Text)).ToList();
  119. break;
  120. case 2:
  121. ChildList = Base.KE.Children.Where(x => x.Names.StartsWith(TBoxFind.Text)).ToList();
  122. break;
  123. case 3:
  124. ChildList = Base.KE.Children.Where(x => x.Patronymic.StartsWith(TBoxFind.Text)).ToList();
  125. break;
  126. case 4:
  127. foreach (Children item in Base.KE.Children)
  128. {
  129. if (item.Groups.Name_group.StartsWith(TBoxFind.Text))
  130. {
  131. ChildList.Add(item);
  132. }
  133. }
  134. break;
  135. default:
  136. ChildList = Base.KE.Children.ToList();
  137. break;
  138. }
  139. if ((bool)ChBPhoto.IsChecked)
  140. {
  141. ChildList = ChildList.Where(x => x.Photo != "\\Resources\\Заглушка.png").ToList();
  142. }
  143. switch (CbSort.SelectedIndex)
  144. {
  145. case 1:
  146. ChildList.Sort((x, y) => x.Surname.CompareTo(y.Surname));
  147. break;
  148. case 2:
  149. ChildList.Sort((x, y) => x.Names.CompareTo(y.Names));
  150. break;
  151. case 3:
  152. ChildList.Sort((x, y) => x.Patronymic.CompareTo(y.Patronymic));
  153. break;
  154. case 4:
  155. ChildList.Sort((x, y) => x.Surname.CompareTo(y.Surname));
  156. ChildList.Reverse();
  157. break;
  158. case 5:
  159. ChildList.Sort((x, y) => x.Names.CompareTo(y.Names));
  160. ChildList.Reverse();
  161. break;
  162. case 6:
  163. ChildList.Sort((x, y) => x.Patronymic.CompareTo(y.Patronymic));
  164. ChildList.Reverse();
  165. break;
  166. }
  167. lv.ItemsSource = ChildList;
  168. if (ChildList.Count != 0)
  169. {
  170. SetPagination();
  171. }
  172. }
  173. private void GoPage_MouseDown(object sender, MouseButtonEventArgs e)
  174. {
  175. TextBlock tb = (TextBlock)sender;
  176. switch (tb.Uid)
  177. {
  178. case "first":
  179. Pagin.CurrentPage = 1;
  180. break;
  181. case "last":
  182. Pagin.CurrentPage = ChildList.Count;
  183. break;
  184. case "back":
  185. Pagin.CurrentPage--;
  186. break;
  187. case "next":
  188. Pagin.CurrentPage++;
  189. break;
  190. default:
  191. Pagin.CurrentPage = Convert.ToInt32(tb.Text);
  192. break;
  193. }
  194. lv.ItemsSource = ChildList.Skip(Pagin.CurrentPage * Pagin.CountPage - Pagin.CountPage).Take(Pagin.CountPage).ToList();
  195. }
  196. private void tboxPageCount_TextChanged(object sender, TextChangedEventArgs e)
  197. {
  198. SetPagination();
  199. }
  200. private void SetPagination()
  201. {
  202. try
  203. {
  204. Pagin.CountPage = Convert.ToInt32(tboxPageCount.Text);
  205. }
  206. catch
  207. {
  208. Pagin.CountPage = ChildList.Count;
  209. }
  210. Pagin.CountList = ChildList.Count;
  211. lv.ItemsSource = ChildList.Skip(0).Take(Pagin.CountPage).ToList();
  212. if (!StartForFilter && !StartForSort)
  213. {
  214. Pagin.CurrentPage = 1;
  215. }
  216. if (!StartForFilter)
  217. {
  218. StartForSort = false;
  219. }
  220. StartForFilter = false;
  221. }
  222. }
  223. }