简体   繁体   中英

How to disable button when the login is false using MVVM,WPF in C#

I am studying the mvvm pattern and ran into a problem, I need to make sure that the button is inactive when the user entered his data incorrectly

I have dialog window (loginPage) where i vote Login and Password

public class LoginViewModel : ViewModel
{
    ApplicationDbContext db;
    IEnumerable<User> users;
    IAuthorizationService _authorizationService;
    IHashingManager _hashingManager;

    private bool? _closeWindow;
    private RelayCommand _loginUser;
    private RelayCommand _cancelCommand;
    public bool EnableClose { get; set; }

    public LoginViewModel(IViewFactory viewFactory, ICore core) : base(viewFactory)
    {
        _authorizationService = core.AuthorizationService;
        _hashingManager = core.HashingManager;
        db = new ApplicationDbContext();
    }

    public ICommand Command { get; set; }


    private string login;
    public string Login
    {
        get => login;
        set
        {
            login = value;
            RaisePropertyChanged(nameof(Login));
        }
    }
    private string password;
    public string Password
    {
        get => password;
        set
        {
            password = value;
            RaisePropertyChanged(nameof(Password));
        }
    }
    public RelayCommand CancelCommand
    {
        get
        {
            return _cancelCommand ??
        (_cancelCommand = new RelayCommand(() =>
            {
                 var result = _authorizationService.Login(Login, _hashingManager.Encrypt(Password));
                 CloseWindow = true;
            }));
        }
    }

    public bool? CloseWindow
    {
        get { return _closeWindow; }
        set
        {
            _closeWindow = value;
            RaisePropertyChanged(nameof(CloseWindow));
        }
    }

    public RelayCommand LoginUser
    {
        get
        {
            return _loginUser ??
                (_loginUser = new RelayCommand(() =>
                {
                    var result = _authorizationService.Login(Login, _hashingManager.Encrypt(Password));
                    CloseWindow = true;
                }));

        }
    }


    public IEnumerable<User> Users
    {
        get { return users; }
        set
        {
            users = value;
            RaisePropertyChanged("Users");
        }
    }
}

My MainWindow VM

public class MainViewModel : ViewModel
{
    private readonly ICore _core;
    public ICommand LoginCommand { get; }

    public ObservableCollection<ILayoutElementViewModel> Documents { get; private set; }
    public MainViewModel(IViewFactory viewFactory, ICore core) : base(viewFactory)
    {
        _core = core;
        this.Documents = new ObservableCollection<ILayoutElementViewModel>();
    }


    ServiceResult _serviceResult = new ServiceResult();
    

    private RelayCommand openChartView;
    public RelayCommand OpenChartView
    {
        get
        {
            return openChartView ??
                (openChartView = new RelayCommand(()=>
                {
                    if (_serviceResult.IsSucceed)
                    {
                        var chartView = new ChartSelectionViewModel(_viewFactory);
                        _viewFactory.ShowDialogView(chartView);
                    }
                }));
        }
    }


    private string _myProperty;
    public string MyProperty
    { 
        get => _myProperty;
        set
        {
            _myProperty = value;
            RaisePropertyChanged(() => MyProperty);
        }
    }

    protected override void LoadedExecute()
    {
        base.LoadedExecute();
        _viewFactory.ShowDialogView(new LoginViewModel(_viewFactory, _core));

    }
}

When i wrote corectly or not, my dialog window close and main window becomes active. There is a button , and i want disable this button when the login and password was incorrect.

I think i must use my ServiceResult in MW

    public class ServiceResult
{
    public bool IsSucceed { get; set; }
    public string Error { get; set; }
    public ServiceResult()
    {
        IsSucceed = true;
    }

    public ServiceResult(string error)
    {
        IsSucceed = false;
        Error = error;
    }
}

or

AuthorizationService

public class AuthorizationService : IAuthorizationService
{
    public ServiceResult Login(string login,string password)
    {
        ApplicationDbContext db = new ApplicationDbContext();

        var listUsers = db.Users.ToList();
        foreach (var item in listUsers)
        {
            if (item.Login == login && item.Password == password)
            {
                return new ServiceResult();
            }
        }
        return new ServiceResult("Wrong Login or Password!");
      }
}

How can i do that? Thx u.

I done, it was so easy

protected override void LoadedExecute()
    {
        base.LoadedExecute();
        var vm = new LoginViewModel(_viewFactory, _core);
        _viewFactory.ShowDialogView(vm);
        IsAuth = vm.AuthorizationResult;
    }


private RelayCommand openChartView;
    public RelayCommand OpenChartView
    {
        get
        {
            return openChartView ??
                (openChartView = new RelayCommand(()=>
                {
                    if (IsAuth)
                    {
                        var chartView = new ChartSelectionViewModel(_viewFactory);
                        _viewFactory.ShowDialogView(chartView);
                    }
                }));
        }
    }

You can do a bool binding to the IsEnable property of the button.

code:

public bool Enabled
  {
      get => _enabled; set
      {
          _enabled = value;
          NotifypropertyChanged("Enabled");
       }
  }

xaml:

<Button IsEnabled="{Binding Enabled}"></Button>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM