Alex P.
Alex P.

Reputation: 3787

WPF MVVM Light: Command.RaiseCanExecuteChanged() doesn't work

I have something like this in my ViewModel:

public enum WorkState
{
    Idle, Working, Stopping
};

public class MainViewModel : ViewModelBase
{
    public const string StatePropertyName = "State";
    private WorkState _state = WorkState.Idle;
    public WorkState State
    {
        get
        { return _state; }
        set
        {
            if (_state == value)
            {
                return;
            }

            RaisePropertyChanging(StatePropertyName);
            _state = value;
            RaisePropertyChanged(StatePropertyName);

            StartStopCommand.RaiseCanExecuteChanged(); // <—————————————
        }
    }


    private RelayCommand _startStopCommand;
    public RelayCommand StartStopCommand
    {
        get
        {
            return _startStopCommand
                   ?? (_startStopCommand = new RelayCommand(
                                  () =>
                                  {
                                      if (State == WorkState.Idle)
                                      {
                                          State = WorkState.Working;
                                      }
                                      else if (State == WorkState.Working)
                                      {
                                          State = WorkState.Stopping;

                                          new Thread(() =>
                                               {
                                                   Thread.Sleep(5000);
                                                   State = WorkState.Idle;
                                                }).Start();
                                      }
                                  },
                                  () => (State == WorkState.Idle ||
                                           (State == WorkState.Working)));
        }
    }
}

And button in my View:

        <Button Command="{Binding StartStopCommand}">
        <Button.Style>
            <Style>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding State}" Value="{x:Static vm:WorkState.Idle}">
                        <Setter Property="Button.Content" Value="Start"/>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding State}" Value="{x:Static vm:WorkState.Working}">
                        <Setter Property="Button.Content" Value="Stop"/>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding State}" Value="{x:Static vm:WorkState.Stopping}">
                        <Setter Property="Button.Content" Value="Stop"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Button.Style>
    </Button>

But when I press Stop button and the State property changes to Idle, button only changes its' text to "Start" but remains disabled until I click somewhere in the window.

I added RaiseCanExecuteChanged to the setter of the State property but it didn't help.

What am I doing wrong?

Also I am not sure that this approach with Start/Stop in the same command and DataTriggers for settings button text is the best so I would be grateful if someone shares better way to do this.

Upvotes: 1

Views: 5163

Answers (3)

Viv
Viv

Reputation: 17380

Your problem here isn't RaiseCanExecuteChanged() not working but modifying your State property from a background thread when your UI depends on it.

So say if I modified your code accordingly to: (This is with .Net 4.5)

public RelayCommand StartStopCommand { get; set; }

...

public MainViewModel() {
  StartStopCommand = new RelayCommand(async () => {
    switch (State) {
      case WorkState.Idle:
        State = WorkState.Working;
        break;
      case WorkState.Working:
        State = WorkState.Stopping;
        await Task.Delay(5000);
        State = WorkState.Idle;
        break;
    }
  }, () => State == WorkState.Idle || State == WorkState.Working);
}

Now you can see the Button switches from disabled back to enabled correctly.

You can get the sample from: Here

If you want to stick with your Thread, switch it to:

new Thread(
  () => {
    Thread.Sleep(5000);
    Application.Current.Dispatcher.BeginInvoke(
      new Action(() => State = WorkState.Idle));
  }).Start();

^^ this would now again work fine since you do not modify the property from another thread.

Upvotes: 3

SvenG
SvenG

Reputation: 5195

I think you have to set your State change via the Dispatcher.

See here on how to do this:

http://msdn.microsoft.com/en-US/magazine/cc163328.aspx#S3 --> (Figure 4 Updating the UI)

Upvotes: 1

thumbmunkeys
thumbmunkeys

Reputation: 20764

call CommandManager.InvalidateRequerySuggested() to reevaluate commands

Upvotes: 1

Related Questions