ICommand interface CanExecute method question

failedtofail

Member
Joined
Jun 16, 2022
Messages
20
Programming Experience
Beginner
My program is based on the MVVM structure. In essence the click of a button kicks of a series of calcs with the answers displayed in textboxes. Button is bound to a property of ICommand type in the ViewModel.

I currently have:

C#:
public bool CanExecute(object parameter)
        {
            return true;
        }

All tutorials I've watched seem to just have the above for the CanExecuteMethod - always returning true. I am looking for an example on how I can modify the return statement of this method to return false once the program has finished executing so that I can activate a list of options for the user such as Redo Calcs, Print Output, Go to Main Menu, etc. once the program has finished.

I have not implemented these yet, but my thoughts are to have these further options being implemented by associated buttons (tied to other ICommands) with these buttons appearing below the outputs once the CanExecute returns false.

Thanks.
 
The most common version of this to not allow the "Save" or "OK" or "Submit" button to be enabled until all the fields have valid values.
 
This C# Corner article seems good and will take me some time to work through. I've never come across Bindable Objects, CommandManager.RequerySuggested, BooleanToVisibilityConverter or Prims before. Managed to install the Prism add-ons successfully but there was still and error.

Had to change

class MainWindowViewModel : BindableBase

to

class MainWindowViewModel : BindableBase, ICommand

and implement the ICommand interface.

Now to learn all those other terms.

Thanks for the link.
 
This C# Corner article seems good and will take me some time to work through. I've never come across Bindable Objects, CommandManager.RequerySuggested, BooleanToVisibilityConverter or Prims before. Managed to install the Prism add-ons successfully but there was still and error.

Had to change

class MainWindowViewModel : BindableBase

to

class MainWindowViewModel : BindableBase, ICommand

and implement the ICommand interface.

Now to learn all those other terms.

Thanks for the link.
Skydiver

Been working through the Icommand and RelayCommand tutorials. I keep on coming across the following code but the tutorial author does not explain what it means and probably just assumes reader has prior knowledge which I do not:

C#:
 public event EventHandler CanExecuteChanged
       {
           add
           {
               CommandManager.RequerySuggested += value;
               this.CanExecuteChangedInternal += value;
           } 

           remove
           {
               CommandManager.RequerySuggested -= value;
               this.CanExecuteChangedInternal -= value;
           }
       }

I understand that the first line declares an even of name CanExecuteChanged which encapsulates a delegate called EvenHandler.

Could you explain what is CommandManager.RequerySuggested please? Also, what is an example of value?

I presume the intention of this block of code is to define the broadcasting.

Thanks.
 
Sorry, I don't know either. I would assume that is to hook into the WPF's eventing so that you get notified that CanExecute may have changed either due to other things happening within the UI (e.g. the CommandManager), or internally when the state of the instance changes. (e.g. the this.CanExecuteChangedInternal).
 
You are halfway there. Follow the link in the second answer that goes to Josh's MVVM blog which explains further.
 
Sorry, I don't know either. I would assume that is to hook into the WPF's eventing so that you get notified that CanExecute may have changed either due to other things happening within the UI (e.g. the CommandManager), or internally when the state of the instance changes. (e.g. the this.CanExecuteChangedInternal).
You are halfway there. Follow the link in the second answer that goes to Josh's MVVM blog which explains further.
Ok, read that thank you. Now I do have a question for you please Skydiver (or anyone else who'd like to contribute):

C#:
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}

1. Could you maybe give an example of what value would be? I understand that value would be a method that subscribes to CommanManager.RequerySuggested.

2. When does add come into play and when does remove come into play?

3. What happened to my own code I used before to fire CanExecuteChanged like in the below basic ICommand structure?


C#:
public bool CanExecute(object parameter)
{
// some code typically an If statement that returns true or false.
return variable;
}

public void Execute(object parameter)
{
//code to be executed
}

public event EventHandler CanExecuteChanged;

Thanks.
 
value would be the delegate that would be called when the event is fired.

add and remove are used to manage the list of multicast event handlers.

You don't have to register for CanExecuteChanged events. It is the WPF UI/Command system that typically registers for the event. You as the ICommand implementer don't have to do anything. If you want to help the system out, you can fire the invent from your view model if there are things that you know will change what your CanExecute() will return.
 

Latest posts

Back
Top Bottom