Please consider using the ICommand
interface. The interface contains ICommand.CanExecute Method that determines whether the command can execute in its current state. An instance of ICommand
interface can be bound to the Command
property of the Button
instance. If the command can not be executed, the button will be disabled automatically.
The ICommand
interface implementation which has RaiseCanExecuteChanged()
-like method must be used to achieve the described behavior, for example:
DelegateCommand
class from Prism library.
RelayCommand
from MVVM Light library.
- etc.
The implementation of ViewModel
using DelegateCommand
class from Prism library:
[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-code:
<Button Content="Activate"
Command="{Binding ActivateCommand}" />
Have not found the documentation about PostSharp's ICommand
-interface support, but a question: INotifyPropertyChanged working with ICommand?,
PostSharp Support.