Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is CanExecuteChanged for?

Tags:

Can I use CanExecuteChanged to change the "can execute" condition?

Or else... "for what" its used?

like image 219
Relativity Avatar asked Dec 25 '10 19:12

Relativity


People also ask

How do you use Canexecutechanged?

Passing a parameter to the CanExecute and Execute methodsA parameter can be passed through the "CommandParameter" property. Once the button is clicked the selected address value is passed to the ICommand. Execute method. The CommandParameter is sent to both CanExecute and Execute events.

What is ICommand in c#?

The ICommand interface is the code contract for commands that are written in . NET for Windows Runtime apps. These commands provide the commanding behavior for UI elements such as a Windows Runtime XAML Button and in particular an AppBarButton .

What is CanExecute?

Defines the method that determines whether the command can execute in its current state.

How to implement ICommand in WPF?

First, create one WPF application and create a window like the following. Now, we have to create one command class along with a View-Model class. View-Model contains some properties like Name. Now, let's implement the View-Model with INotifyProperyChanged interface.


1 Answers

No you can not use it to change the can execute state. It is an event and objects which participate in the ICommand pattern can choose to listen to this event e.g. a button may use this event to know when to re-query the commands state (by calling the can execute method) to set its enabled state.

In order for the can execute pattern to be useful there needs to be something that can be used to raise the event. Prism's DelegateCommand has a method you can call to manually raise this event so subscribers will re-query the can execute method if they have opted into the pattern.

  • Assign command to button.
  • Button subscribes to can execute changed event.
  • Button execute can execute method and it returns false - disables button.
  • You change state that can execute method depends on.
  • You call raise can execute changed on Prism command.
  • Can execute changed event is raised.
  • Button event handler fires.
  • Button calls command can execute method - button is enabled.

Example

In the following Prism based example we change the state of SaveCommand CanExecute from false to true whilst the save command is executing. The call toRaiseCanExecuteChanged will cause the CanExecuteChanged event to be raised, and clients to call the CanExecute method. In practice this would make a Save button that was bound to SaveCommand change its state from enabled to disabled and back to enabled again.

public class BlingViewModel
{
    private DelegateCommand<object> _saveCommand;
    private bool _canSaveExecute = true;

    public ICommand SaveCommand
    {
        get
        {
            if (_saveCommand == null)
            {
                _saveCommand = new DelegateCommand<object>
                    (
                    executeMethod: _ => Save()
                    ,
                    canExecuteMethod: _ => _canSaveExecute
                    );
            }
            return _saveCommand;
        }
    }

    private void Save()
    {
        _canSaveExecute = false;
        _saveCommand.RaiseCanExecuteChanged();

        Console.WriteLine("Saving...");

        _canSaveExecute = true;
        _saveCommand.RaiseCanExecuteChanged();
    }
}
like image 60
Tim Lloyd Avatar answered Oct 13 '22 00:10

Tim Lloyd