wpf CanExecuteChanged 有什么用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4531360/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is CanExecuteChanged for?
提问by Relativity
Can I use CanExecuteChanged to change the "can execute" condition?
我可以使用 CanExecuteChanged 来更改“可以执行”条件吗?
Or else... "for what" its used?
或者……“为了什么”它使用?
回答by Tim Lloyd
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.
不,您不能使用它来更改可以执行状态。它是一个事件,参与ICommand
模式的对象可以选择侦听此事件,例如按钮可以使用此事件来了解何时重新查询命令状态(通过调用 can execute 方法)以设置其启用状态。
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.
为了使 can execute 模式有用,需要有一些可以用来引发事件的东西。PrismDelegateCommand
有一个方法,您可以调用它来手动引发此事件,因此订阅者如果选择了该模式,将重新查询 can execute 方法。
- 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.
- 将命令分配给按钮。
- 按钮订阅可以执行更改的事件。
- 按钮执行可以执行方法并返回 false - 禁用按钮。
- 您更改可以执行方法取决于的状态。
- 您调用 raise 可以在 Prism 命令上执行更改。
- 可以执行已引发更改的事件。
- 按钮事件处理程序触发。
- 按钮调用命令可以执行方法 - 按钮已启用。
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.
在以下基于 Prism 的示例中,我们在CanExecute
执行 save 命令时将SaveCommand 的状态从 false更改为 true。调用RaiseCanExecuteChanged
将导致引发CanExecuteChanged
事件,并且客户端调用该CanExecute
方法。在实践中,这将使一个保存按钮必须SaveCommand
将其状态从启用更改为禁用,然后再次返回启用。
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();
}
}
回答by Jodrell
Or just call System.Windows.Input.CommandManager.InvalidateRequerySuggested()
so that your CanExecute
handlers are re-evaluated.
或者只是调用System.Windows.Input.CommandManager.InvalidateRequerySuggested()
以便CanExecute
重新评估您的处理程序。
回答by Thomas
public override event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
I don't know what the performance implications are for this; however, they don't seem to be too great, either way. This is what I use.
我不知道这对性能有什么影响;然而,无论哪种方式,它们似乎都不太好。这就是我使用的。