MainWindowViewModel.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using MVVM;
  2. using System.Collections.ObjectModel;
  3. using System.Linq;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. namespace Work29
  7. {
  8. public class MainWindowViewModel : BaseViewModel
  9. {
  10. private ObservableCollection<User> _users;
  11. private User _user;
  12. private RelayCommand _autoReady;
  13. private RelayCommand _regNew;
  14. public RelayCommand AutoReady
  15. {
  16. get
  17. {
  18. return _autoReady ??
  19. (_autoReady = new RelayCommand(x =>
  20. {
  21. if (x is PasswordBox password)
  22. {
  23. Work29Context wpfContext = new Work29Context();
  24. User user = Users.FirstOrDefault(p => p.Login == User.Login && p.Password == password.Password);
  25. if (user != null)
  26. {
  27. User.a_user = user;
  28. UserWindow userwindow = new UserWindow();
  29. userwindow.Show();
  30. foreach (var window in App.Current.Windows)
  31. {
  32. if (window is MainWindow mainWindow)
  33. {
  34. mainWindow.Close();
  35. }
  36. }
  37. }
  38. else
  39. {
  40. MessageBox.Show("Ошибка авторизации!!!");
  41. }
  42. }
  43. }
  44. ));
  45. }
  46. }
  47. public RelayCommand RegNew
  48. {
  49. get
  50. {
  51. return _regNew ??
  52. (_regNew = new RelayCommand(x =>
  53. {
  54. RegWindow userWindow = new RegWindow();
  55. userWindow.Show();
  56. foreach (var window in App.Current.Windows)
  57. {
  58. if (window is MainWindow mainWindow)
  59. {
  60. mainWindow.Close();
  61. }
  62. }
  63. }
  64. ));
  65. }
  66. }
  67. public User User
  68. {
  69. get => _user;
  70. set
  71. {
  72. _user = value;
  73. OnPropertyChanged();
  74. }
  75. }
  76. public ObservableCollection<User> Users
  77. {
  78. get => _users;
  79. set
  80. {
  81. _users = value;
  82. OnPropertyChanged();
  83. }
  84. }
  85. public MainWindowViewModel()
  86. {
  87. Work29Context context = new Work29Context();
  88. _user = new User();
  89. _users = new ObservableCollection<User>(context.Users);
  90. }
  91. }
  92. }