AddEditPage.xaml.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15. using System.Linq.Expressions;
  16. using System.Data.Entity.Migrations;
  17. namespace ROGOZ.Pages
  18. {
  19. /// <summary>
  20. /// Логика взаимодействия для AddEditPage.xaml
  21. /// </summary>
  22. public partial class AddEditPage : Page
  23. {
  24. private user1Entities _context = new user1Entities();
  25. private Task _currentTask = null;
  26. public AddEditPage()
  27. {
  28. InitializeComponent();
  29. DataContext = _currentTask;
  30. CmbStatus.ItemsSource = Status.FillStatus();
  31. CmbTaskType.ItemsSource = WorkType.FillWorkType();
  32. //CmbExecutor.ItemsSource = user1Entities.GetContext().Executor.ToList();
  33. CmbExecutor.ItemsSource = _context.User.Where(u=>u.Executor != null).ToList();
  34. }
  35. public AddEditPage(Task selectedTask)
  36. {
  37. InitializeComponent();
  38. _currentTask = selectedTask;
  39. CmbStatus.ItemsSource = Status.FillStatus();
  40. //CmbExecutor.ItemsSource = user1Entities.GetContext().Executor.ToList();
  41. CmbExecutor.ItemsSource = _context.User.Where(u => u.Executor != null).ToList();
  42. CmbTaskType.ItemsSource = WorkType.FillWorkType();
  43. if (selectedTask != null)
  44. _currentTask = selectedTask;
  45. DataContext = _currentTask;
  46. }
  47. private void BtnCancel_Click(object sender, RoutedEventArgs e)
  48. {
  49. NavigationService.GoBack();
  50. }
  51. private string CheckErrors()
  52. {
  53. var errorBuilder = new StringBuilder();
  54. if (string.IsNullOrWhiteSpace(TaskNameTB.Text))
  55. errorBuilder.AppendLine("Укажите название заголовка!");
  56. if (CmbStatus.Text == null)
  57. errorBuilder.AppendLine("Выберите статус!");
  58. if (CmbTaskType.Text == null)
  59. errorBuilder.AppendLine("Выберите характеристику!");
  60. if (errorBuilder.Length > 0) //проверка на ошибку
  61. {
  62. MessageBox.Show(errorBuilder.ToString());
  63. return errorBuilder.ToString();
  64. }
  65. else return null;
  66. }
  67. private void BtnSaveTask_Click(object sender, RoutedEventArgs e)
  68. {
  69. if (CheckErrors() == null)
  70. {
  71. if (_currentTask == null)
  72. {
  73. try
  74. {
  75. var selectedTask = new Task
  76. {
  77. //ExecutorID = int.Parse(CmbExecutor.SelectedItem.ToString()),
  78. ExecutorID = (CmbExecutor.SelectedItem as User)?.ID ?? 0,
  79. //ExecutorID =int.Parse(CmbExecutor.Text),
  80. Title = TaskNameTB.Text,
  81. Description = TaskDescriptionTB.Text,
  82. CreateDateTime = DateTime.Parse(DPCreateDateTime.Text),
  83. Deadline = DateTime.Parse(DPDeadLine.Text),
  84. Difficulty = Double.Parse(DifficultyTB.Text),
  85. Time = int.Parse(TimeTB.Text),
  86. Status = CmbStatus.Text,
  87. WorkType = CmbTaskType.Text,
  88. IsDeleted = false
  89. };
  90. user1Entities.GetContext().Task.Add(selectedTask);
  91. user1Entities.GetContext().SaveChanges();
  92. MessageBox.Show("Задача добавлена");
  93. NavigationService.GoBack();
  94. }
  95. catch
  96. {
  97. MessageBox.Show(CheckErrors(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
  98. }
  99. }
  100. else
  101. {
  102. var context = user1Entities.GetContext();
  103. context.SaveChanges();
  104. MessageBox.Show("Задача отредактирована");
  105. NavigationService.GoBack();
  106. }
  107. }
  108. }
  109. private void DifficultyTB_TextInput(object sender, TextCompositionEventArgs e)
  110. {
  111. if (!char.IsDigit(e.Text, e.Text.Length - 1))
  112. {
  113. e.Handled = true;
  114. }
  115. }
  116. private void DifficultyTB_TextChanged(object sender, TextChangedEventArgs e)
  117. {
  118. if (sender is TextBox diffTB)
  119. {
  120. if (string.IsNullOrEmpty(diffTB.Text)) { return; }
  121. if (!int.TryParse(diffTB.Text, out int number) || number < 1 || number >50)
  122. {
  123. DifficultyTB.Clear();
  124. MessageBox.Show("Пожалуйста, введите число от 1 до 50.");
  125. }
  126. }
  127. }
  128. }
  129. }