WPF – Continuous CANEXECUTE Call in WPF – Command

I am applying the MVVM pattern to a project. I have a UserControl which has a button which is bound to a command exposed by the ViewModel.
Since the button is visible, It will continue to call the button’s CanExecute method. Something tells me that this will cause a performance loss, but I’m not sure. Is this the expected behavior? Or is there a better way to bind buttons to commands?

Thank you.

Sorry, I found out what happened Matter.
This is the implementation of RelayCommand.

public class RelayCommand: ICommand
{
#region Fields
< br /> readonly Action _execute;
readonly Predicate _canExecute;

#endregion // Fields

#region Constructors
< br /> public RelayCommand(Action execute)
: this(execute, null)
{
}

public RelayCommand(Action execute, Predicate canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute");

_execute = execute;
_canExecute = canExecute;
}
#endregion // Constructors

#region ICommand Members

[DebuggerStepThrough]
public bool CanExecute( object parameter)
{
return _canExecute == null? true: _canExecute(parameter);
}

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

public void Execute(object parameter)
{
_execute(parameter);
}

#endregion // ICommand Members
}

I mistakenly believe that the system automatically re-query all commands. What it actually does is hook the CanExecuteChanged event of each Command, and RelayCommand Basically link its CanExecuteChanged event to the CommandManager’s RequerySuggested event, so every time the system “suggests” a requery, it is actually requerying all of my RelayCommands.

Thank you.

< /div>

I am applying the MVVM pattern to the project. I have a UserControl which has a button which is bound to a command exposed by the ViewModel.
Since the button is visible, it is The button’s CanExecute method is continuously called. Something tells me that this will cause performance loss, but I’m not sure. Is this the expected behavior? Or is there a better way to bind buttons to commands?

Thank you.

Sorry, I found out what happened.
This is the implementation of RelayCommand. < p>

public class RelayCommand: ICommand
{
#region Fields

readonly Action _execute;
readonly Predicate _canExecute;

#endregion // Fields

#region Constructors

public RelayCommand(Action execute)
: this(execute, null)
{
}

public RelayCommand(Action execute, Predicate canExecute)
{< br /> if (execute == null)
throw new ArgumentNullException("execute");

_execute = execute;
_canExecute = canExecute;
}
#endregion // Constructors

#region ICommand Members

[DebuggerStepThrough]
public bool CanExecute(object parameter)
{
return _canExecute == null? true: _canExecute(parameter);
}

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

public void Execute(object parameter)
{
_execute(parameter);
}

#endregion // ICommand Members
}

< p>I mistakenly think that the system automatically re-query all commands. What it actually does is hook the CanExecuteChanged event of each Command, and RelayCommand basically links its CanExecuteChanged event to the RequerySuggested event of the CommandManager, so every time the system “suggests” When a requery is made, it is actually requerying all of my RelayCommands.

Thank you.

Leave a Comment

Your email address will not be published.