RegWindowViewModel.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using MVVM;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Collections.ObjectModel;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. namespace Work29.ViewModels
  11. {
  12. public class RegWindowViewModel : BaseViewModel
  13. {
  14. private User _user;
  15. private RelayCommand _createUser;
  16. public RelayCommand CreateUser
  17. {
  18. get
  19. {
  20. return _createUser ??
  21. (_createUser = new RelayCommand(x =>
  22. {
  23. string stroka = "";
  24. User user = User;
  25. if ((User.Surname != stroka) && (User.Name != stroka)
  26. && (User.Patronimyc != stroka) && (User.Login != stroka)
  27. && (User.Password != stroka) && (User.Phone != stroka))
  28. {
  29. Work29Context context = new Work29Context();
  30. context.Users.Add(user);
  31. context.SaveChanges();
  32. user = new User();
  33. MainWindow mainWindow = new MainWindow();
  34. mainWindow.Show();
  35. foreach (var window in App.Current.Windows)
  36. {
  37. if (window is RegWindow regWindow)
  38. {
  39. regWindow.Close();
  40. }
  41. }
  42. }
  43. else
  44. {
  45. MessageBox.Show("Все поля должны быть заполнены!");
  46. }
  47. }
  48. ));
  49. }
  50. }
  51. public User User
  52. {
  53. get => _user;
  54. set
  55. {
  56. _user = value;
  57. OnPropertyChanged();
  58. }
  59. }
  60. public RegWindowViewModel()
  61. {
  62. Work29Context context = new Work29Context();
  63. _user = new User();
  64. }
  65. }
  66. }