Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ICommands and the "CanExecuteMethod" of a DelegateCommand Why doesn't it work for us?

We're trying to use an ICommand to set up a button in Silverlight with Prism. We'd like the button to be disabled on occasion. DelegateCommand takes 2 parameters, an "ExecuteMethod" and a "CanExecuteMethod"

When we set up the ICommand we expect that if the "CanExecuteMethod" is used, then it will be called to see if the "ExecuteMethod" can be called. The button's Enabled state should reflect the result of the "CanExecuteMethod"

What we actually see: When the form is created, the method is called and the button is enabled or disabled. (in this case, Enabled) The CanExecuteMethod is never called again and the Execute method will fire even though we've tried to set behavior to keep that from happening. Execption is thrown (what we'd like to avoid).

The obvious answer is that we should be calling some sort of :

OnPropertyChanged("SaveCommand");

but we're doing it wrong somehow. Either we're assuming that it works a way that it doesn't, or we're missing a step. Any Ideas?

Code:

SaveCommand = new DelegateCommand<string>(OnSaveCommand, CanSave);

public void OnSaveCommand( string helpNumber )
        {
            OnPropertyChanged("SaveCommand");
           //DoSaveStuff
        }

        public bool CanSave(Object sender)
        {
            return Model.CanSave();// true or false depending
        }
like image 868
thepaulpage Avatar asked Feb 28 '23 07:02

thepaulpage


1 Answers

Your SaveCommand, because it is a DelegateCommand, has a function called RaiseCanExecuteChanged().

When you call this function it will have the control refresh from the CanSave function.

The OnPropertyChanged equal for DelegateCommands is MyCommand.RaiseCanExecuteChanged.

Have fun!

like image 196
Jeremiah Avatar answered Apr 06 '23 03:04

Jeremiah