我有点纠结于Telerik.Windows.Controls DelegateCommand的正确用法
我有下面的设置,它可以编译,但我更关心的是,我是否正确地使用了它。我已经在网上搜索了一段时间,没有找到任何例子。
特别是,我对如何使用CanSaveAuthorization或底层CanExecute,以及如何处理所需的object参数感到困惑。
谢谢,
public class CreateAuthorizationViewModel : ViewModelBase
{
private Authorization authorization;
private AuthorizationRepository authorizationRepository;
private DelegateCommand saveAuthorizationCommand;
public DelegateCommand SaveAuthorizationCommand
{
get
{
return saveAuthorizationCommand;
}
}
public CreateAuthorizationViewModel()
{
InitializeCommand();
}
private void InitializeCommand()
{
saveAuthorizationCommand = new DelegateCommand(SaveAuthorization, CanSaveAuthorization);
}
private void SaveAuthorization(object parameter)
{
authorizationRepository.Save();
}
private bool CanSaveAuthorization(object parameter)
{
//I would have validation logic here
return true;
}
}发布于 2013-02-02 18:19:54
DelegateCommand实现ICommand接口。这意味着它可以绑定到像Button这样的WPF控件的Command属性。CanExecute方法(在您的示例中为CanSaveAuthorization)可以评估是否允许执行execute方法(在您的示例中为SaveAuthorization),如果不允许,则该按钮在视图中将被禁用。object类型的参数在这里可能会有帮助。我从未使用过Telerik的实现,但我认为这是可以在视图中设置的控件的CommandParameter属性的值。如果您有一个总是返回true的CanExecute方法,那么您不妨将其全部删除。
如果你在RelayCommand上搜索,你可能会找到更多的信息和例子。这大概就是Telerik基于DelegateCommand的模式。我的DelegateCommand版本在没有parameter参数的情况下有一个重载。然后,CanExecute方法需要视图模型中的可用信息来确定CanExecute状态。
发布于 2013-02-05 22:27:06
这是一个小问题,但您可以摆脱对InitializeCommand函数的需要:
public DelegateCommand SaveAuthorizationCommand
{
get
{
return saveAuthorizationCommand ??
(saveAuthorizationCommand = new DelegateCommand(SaveAuthorization, CanSaveAuthorization));
}
}https://stackoverflow.com/questions/14657507
复制相似问题