using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Linq.Expressions; using System.Data.Entity.Migrations; namespace ROGOZ.Pages { /// /// Логика взаимодействия для AddEditPage.xaml /// public partial class AddEditPage : Page { private user1Entities _context = new user1Entities(); private Task _currentTask = null; public AddEditPage() { InitializeComponent(); DataContext = _currentTask; CmbStatus.ItemsSource = Status.FillStatus(); CmbTaskType.ItemsSource = WorkType.FillWorkType(); //CmbExecutor.ItemsSource = user1Entities.GetContext().Executor.ToList(); CmbExecutor.ItemsSource = _context.User.Where(u=>u.Executor != null).ToList(); } public AddEditPage(Task selectedTask) { InitializeComponent(); _currentTask = selectedTask; CmbStatus.ItemsSource = Status.FillStatus(); //CmbExecutor.ItemsSource = user1Entities.GetContext().Executor.ToList(); CmbExecutor.ItemsSource = _context.User.Where(u => u.Executor != null).ToList(); CmbTaskType.ItemsSource = WorkType.FillWorkType(); if (selectedTask != null) _currentTask = selectedTask; DataContext = _currentTask; } private void BtnCancel_Click(object sender, RoutedEventArgs e) { NavigationService.GoBack(); } private string CheckErrors() { var errorBuilder = new StringBuilder(); if (string.IsNullOrWhiteSpace(TaskNameTB.Text)) errorBuilder.AppendLine("Укажите название заголовка!"); if (CmbStatus.Text == null) errorBuilder.AppendLine("Выберите статус!"); if (CmbTaskType.Text == null) errorBuilder.AppendLine("Выберите характеристику!"); if (errorBuilder.Length > 0) //проверка на ошибку { MessageBox.Show(errorBuilder.ToString()); return errorBuilder.ToString(); } else return null; } private void BtnSaveTask_Click(object sender, RoutedEventArgs e) { if (CheckErrors() == null) { if (_currentTask == null) { try { var selectedTask = new Task { //ExecutorID = int.Parse(CmbExecutor.SelectedItem.ToString()), ExecutorID = (CmbExecutor.SelectedItem as User)?.ID ?? 0, //ExecutorID =int.Parse(CmbExecutor.Text), Title = TaskNameTB.Text, Description = TaskDescriptionTB.Text, CreateDateTime = DateTime.Parse(DPCreateDateTime.Text), Deadline = DateTime.Parse(DPDeadLine.Text), Difficulty = Double.Parse(DifficultyTB.Text), Time = int.Parse(TimeTB.Text), Status = CmbStatus.Text, WorkType = CmbTaskType.Text, IsDeleted = false }; user1Entities.GetContext().Task.Add(selectedTask); user1Entities.GetContext().SaveChanges(); MessageBox.Show("Задача добавлена"); NavigationService.GoBack(); } catch { MessageBox.Show(CheckErrors(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error); } } else { var context = user1Entities.GetContext(); context.SaveChanges(); MessageBox.Show("Задача отредактирована"); NavigationService.GoBack(); } } } private void DifficultyTB_TextInput(object sender, TextCompositionEventArgs e) { if (!char.IsDigit(e.Text, e.Text.Length - 1)) { e.Handled = true; } } private void DifficultyTB_TextChanged(object sender, TextChangedEventArgs e) { if (sender is TextBox diffTB) { if (string.IsNullOrEmpty(diffTB.Text)) { return; } if (!int.TryParse(diffTB.Text, out int number) || number < 1 || number >50) { DifficultyTB.Clear(); MessageBox.Show("Пожалуйста, введите число от 1 до 50."); } } } } }