请考虑使用ICommand
接口。该接口包含ICommand.CanExecute Method,用于确定命令是否可以在当前状态下执行。 ICommand
接口的一个实例可以绑定到Button
实例的Command
属性。如果命令无法执行,该按钮将被自动禁用。
的具有RaiseCanExecuteChanged()
样方法ICommand
接口的实现必须被用来实现所描述的行为,例如:从棱镜库
DelegateCommand
类。
RelayCommand
来自MVVM Light库。
- 等
的ViewModel
使用DelegateCommand
类从棱镜库的实现:
[NotifyPropertyChanged]
public class ActivateViewModel
{
private readonly DelegateCommand activateCommand;
private string password;
public ActivateViewModel()
{
activateCommand = new DelegateCommand(Activate,() => !string.IsNullOrEmpty(Password));
}
public string Password
{
get { return password; }
set
{
password = value;
activateCommand.RaiseCanExecuteChanged(); // To re-evaluate CanExecute.
}
}
public ICommand ActivateCommand
{
get { return activateCommand; }
}
private void Activate()
{
// ...
}
}
XAML代码:
<Button Content="Activate"
Command="{Binding ActivateCommand}" />
没有找到关于PostSharp的ICommand
接口向文档支持,但一个问题:INotifyPropertyChanged working with ICommand?, PostSharp Support。
这应该与PS开箱即用。请,你可以在这里发布你的xaml吗?你使用什么类型的项目(WPF,Silverlight,WP等)? – 2015-02-27 11:51:34