14

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

Dante
  • 3,208
  • 9
  • 38
  • 56

1 Answers1

15

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();
}
nemesv
  • 138,284
  • 16
  • 416
  • 359
  • thank you for your reply. I have tried waht you just wrote but my ICommand object does not have such method, It has the event `CanExecuteChanged`, do I need to add a method to it? – Dante Jun 06 '12 at 20:58
  • 1
    You already have this method, you just need to cast back your `DoSomethingCommand` from `ICommand` to `DelegateCommand`. In your sample try it with `((DelegateCommand)DoSomethingCommand).RaiseCanExecuteChanged();` – nemesv Jun 06 '12 at 21:01
  • Or you can change the type of `DoSomethingCommand` from `ICommand` to `DelegateCommand`. – nemesv Jun 06 '12 at 21:01
  • I am struggling with the same issue, I am wondering how this can be done with a TextBox, where the RaiseCanExecuteChanged must be called with every keystroke in the TextBox (after the TextChanged event)..... – Patrick Peters Aug 29 '12 at 20:31
  • RaiseCanExecuteChanged does not work (does not enable or disable the button at run time) for me. – Scott Hutchinson Dec 28 '16 at 23:18
  • Are you calling RaiseCanExecuteChanged on the correct thread? There may be something more complex as to why it does not work. – rollsch Sep 02 '17 at 07:01