public class DelCmd : ICommand { public event EventHandler CanExecuteChanged { add { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested -= value; } } private Action<object> _execute; private Predicate<object> _canExecute; public DelCmd(Action<object> executeValue, Predicate<object> canExecuteValue) { _execute = executeValue; _canExecute = canExecuteValue; } public DelCmd(Action<object> executeValue) : this(executeValue, null) { _execute = executeValue; } public bool CanExecute(object parameter) { if (_canExecute == null) { return true; } return _canExecute(parameter); } public void Execute(object parameter) { _execute(parameter); } }
private DelCmd clearCmd; public DelCmd ClearCmd { get { if(clearCmd==null) { clearCmd = new DelCmd(ClearCmdExecuted, ClearCmdCanExecute); } return clearCmd; } } private bool ClearCmdCanExecute(object obj) { return !string.IsNullOrWhiteSpace(_imagePath) && System.IO.File.Exists(_imagePath); } private void ClearCmdExecuted(object obj) { ImagePath = string.Empty; }
WPF raises the CommandManager.RequerySuggested static event when it thinks the execution state of commands may change. This is typically done after some UI actions that may cause something to change in all bound commands. The following implementation causes WPF to call register for its own notification of re-executing CanExecute for all commands that raise the CanExecuteChange event:
public event EventHandler CanExecuteChanged { add { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested -= value; } }
标签:ICommand,executeValue,DelegateCommand,private,Prism,RequerySuggested,CommandMana From: https://www.cnblogs.com/Fred1987/p/18212998