I am new to using Commands and was trying the CanExecute to enable and disable my buttons depending on some factors. 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. Button on UI is disabled. But from ther it never hits the CanExecuteGenerate again, resulting in my button to stay disabled. 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
}
}