WPF-Prism CanExecute method not being called

前端 未结 4 1334
南方客
南方客 2021-02-01 19:06

I am coding a simple login UserControl with two TextBoxes (Username and Password) and a Login button. I want the Login button to be enabled only when the username and password f

4条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-01 19:36

    Starting with Prism6 the DelegateCommand can "observe" your propertys. Means everytime your property is changing the CanExecute-Method is called. The good thing is you get rid of RaiseCanExecuteChanged in the Propertysetter. You can also chain-call that method if you want to observe more properties:

    public LoginViewModel()
    {
        this.LoginCommand =
            new DelegateCommand(
                this.LoginExecute, this.CanLoginExecute).ObservesProperty(() => Username).ObservesProperty(() => Password);
    }
    
    
    

    Furthermore if you just want your DelegateCommand be called depending on the state of a boolean property you can use .ObservesCanExecute(()=> BoolProp)

    public LoginViewModel()
    {
        this.LoginCommand =
            new DelegateCommand(
                this.LoginExecute).ObservesCanExecute(()=> IsServerOnline).ObservesProperty(() => Username).ObservesProperty(() => Password);
    }
    
    
    

    You dont need this.CanLoginExecute anymore.

    提交回复
    热议问题