How to enable a Button with its CanExecute method
button, c#, icommand, prism, wpf
Solution
There is an event called CanExecuteChanged on the `ICommand` interface which:
Occurs when changes occur that affect whether or not the command should execute.
With the Prism DelegateCommand you can raise this event with the `RaiseCanExecuteChanged` method:
public void SomeMethod()
{
//do some work
isDoSomethingButtonEnabled = true;
DoSomethingCommand.RaiseCanExecuteChanged();
}
Problem
I am developing an application in WPF using MVVM, but I am stuck with the ICommand objects. I have a windows which contains some buttons, so, I bind them to their respective ICommand in XAML as below: ``` <Button Command="{Binding DoSomethingCommand}" Content="Do Something" /> ``` Then, In my view-model class I have written the following: ``` public class MyViewModel : ObservableObject { private bool isDoSomethingButtonEnabled = false; .... public ICommand DoSomethingCommand { get; private set; } .... .... public MyViewModel() { DoSomethingCommand = new DelegateCommand<String>(this.OnDoSomething, this.CanDoSomething); } private void OnDoSomething(String arg) { } private bool CanDoSomething(String arg) { return isDoSomethingButtonEnabled; } .... } ``` So, Since I need that my button is not enabled the first time the window opens, I set my variable `isDoSomethingButtonEnabled` to `false`. And it works, the button is disabled at the beginning, but my problem is that when I change the variable `isDoSomethingButtonEnabled` to `true` at run time my button is still disabled. I have even done some tests after changing the variable `isDoSomethingButtonEnabled` to `true`, printing the result of `DoSomethingCommand.CanExecute()` and it shows "true"! so, what Should I do in order to enable my button?? Thank you in advance