MainWindowViewModel.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 WpfApp29.Models;
  9. namespace WpfApp29.ViewModels
  10. {
  11. internal class MainWindowViewModel : BaseViewModel
  12. {
  13. public User? _user;
  14. public User User
  15. {
  16. get => _user ??= new User();
  17. set
  18. {
  19. _user = value;
  20. OnPropertyChanged();
  21. }
  22. }
  23. public RelayCommand? _commandLogin;
  24. public RelayCommand CommandLogin
  25. {
  26. get
  27. {
  28. return _commandLogin ??= new RelayCommand(
  29. x =>
  30. {
  31. User.Password = (x as PasswordBox)?.Password ?? "";
  32. MainContext ctx = new MainContext();
  33. User? user = ctx.Users.FirstOrDefault(u => u.Login == User.Login && u.Password == User.Password);
  34. if (user == null)
  35. {
  36. MessageBox.Show("Вы ввели неверные логин или пароль");
  37. return;
  38. }
  39. User.CurrentUser = user;
  40. new UserWindow().Show();
  41. AppHelper.GetWindowAtType<MainWindow>()?.Close();
  42. }
  43. );
  44. }
  45. }
  46. public RelayCommand? _commandGoRegister;
  47. public RelayCommand? CommandGoRegister
  48. {
  49. get
  50. {
  51. return _commandGoRegister ??= new RelayCommand(
  52. x =>
  53. {
  54. new RegistrationWindow().Show();
  55. AppHelper.GetWindowAtType<MainWindow>()?.Close();
  56. }
  57. );
  58. }
  59. }
  60. }
  61. }