And*_* K. 4 wpf prism delegatecommand
提前致谢!
我应该如何在PRISM 6的DelegateCommand中使用ObservesCanExecute?
public partial class UserAccountsViewModel: INotifyPropertyChanged
{
public DelegateCommand InsertCommand { get; private set; }
public DelegateCommand UpdateCommand { get; private set; }
public DelegateCommand DeleteCommand { get; private set; }
public UserAccount SelectedUserAccount
{
get;
set
{
//notify property changed stuff
}
}
public UserAccountsViewModel()
{
InitCommands();
}
private void InitCommands()
{
InsertCommand = new DelegateCommand(Insert, CanInsert);
UpdateCommand = new DelegateCommand(Update,CanUpdate).ObservesCanExecute(); // ???
DeleteCommand = new DelegateCommand(Delete,CanDelete);
}
//----------------------------------------------------------
private void Update()
{
//...
}
private bool CanUpdate()
{
return SelectedUserAccount != null;
}
//.....
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,我不熟悉c#中的表达式.另外,我认为这对其他人有帮助.
ObservesCanExecute()作品"大多喜欢"的canExecuteMethod参数DelegateCommand(Action executeMethod, Func<bool> canExecuteMethod).
但是,如果您有布尔属性而不是方法,则无需定义canExecuteMethodwith ObservesCanExecute.
在你的例子中,假设这CanUpdate不是一个方法,只是假设它是一个布尔属性.
然后,您可以更改代码ObservesCanExecute(() => CanUpdate)和DelegateCommand如果将只执行CanUpdate布尔属性的值为true(不需要定义一个方法).
ObservesCanExecute就像是属性上的"快捷方式",而不必定义方法并将其传递给构造函数的canExecuteMethod参数DelegateCommand.
| 归档时间: |
|
| 查看次数: |
3983 次 |
| 最近记录: |