简体   繁体   English

MVVM Light RelayCommand不起作用

[英]MVVM Light RelayCommand not working

I am new to using Commands and was trying the CanExecute to enable and disable my buttons depending on some factors. 我是使用Commands的新手,并根据某些因素尝试使用CanExecute启用和禁用我的按钮。 But I am doing something wrong and cant figure it out. 但是我做错了事,无法解决。 When loading it works fine. 加载时工作正常。 The CanExecuteGenerate Function gets hit, the model is null, thus returning false. CanExecuteGenerate函数被命中,模型为null,因此返回false。 Button on UI is disabled. UI上的按钮已禁用。 But from ther it never hits the CanExecuteGenerate again, resulting in my button to stay disabled. 但是从那里开始,它再也不会击中CanExecuteGenerate,导致我的按钮保持禁用状态。 Can anyone see what I am missing or doing wrong? 谁能看到我的缺失或做错了什么?

public class MainWindowViewModel: PropertyChangedNotification{   
    public RelayCommand GenerateCommand { get; set; }

    public MainWindowViewModel( ) {
    GenerateCommand = new RelayCommand( OnGenerateClicked, CanExecuteGenerate( ) );
    Model = new MainModel( );
    }

    private Func<bool> CanExecuteGenerate( ) {

    if( Model != null ) {
        return ( ) => ( Model.Name != "" && Model.Title != "" ) ? true : false;
      }
        return ( ) => false;
     } 

    public void someothermethod(){
        Model.Name = "James"
        Model.Title = "Doctor"
        GenerateCommand.RaiseCanExecuteChanged();
    }
    public void OnGenerateClicked(){
        //Do some other stuff
    }



}

When you create the RelayCommand you always pass the method that returns false. 创建RelayCommand您总是传递返回false的方法。

You should not create a separate method for the case when model is null, but to handle it in the method you are passing to the RelayCommand . 对于model为null的情况,您不应为该情况创建单独的方法,而应在传递给RelayCommand的方法中对其进行RelayCommand

Try using this method: 尝试使用此方法:

private bool CanExecuteGenerate( ) {
    if( Model != null ) {
        return Model.Name != "" && Model.Title != "";
    }

    return false;
} 

And change the construction of the RelayCommand to 并将RelayCommand的构造更改为

GenerateCommand = new RelayCommand(OnGenerateClicked, CanExecuteGenerate);

Because your CanExecuteGenerate method returns a delegate that will be called. 因为您的CanExecuteGenerate方法返回了将被调用的委托。 Try this one: 试试这个:

public class MainWindowViewModel: PropertyChangedNotification{   
   public RelayCommand GenerateCommand { get; set; }

   public MainWindowViewModel( ) {
   GenerateCommand = new RelayCommand( OnGenerateClicked, CanExecuteGenerate);
   Model = new MainModel( );
   }

   private bool CanExecuteGenerate( ) {
       if( Model != null )
           return ( Model.Name != "" && Model.Title != "" ) ? true : false;
       return  false;
   }

   public void someothermethod(){
       Model.Name = "James"
       Model.Title = "Doctor"
       GenerateCommand.RaiseCanExecuteChanged();
   }
   public void OnGenerateClicked(){
       //Do some other stuff
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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