1

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.

Viv
  • 17,170
  • 4
  • 51
  • 71
Alex P.
  • 3,697
  • 9
  • 45
  • 110

3 Answers3

3

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.

Viv
  • 17,170
  • 4
  • 51
  • 71
1

call CommandManager.InvalidateRequerySuggested() to reevaluate commands

thumbmunkeys
  • 20,606
  • 8
  • 62
  • 110
  • I tried it before .RaiseCanExecuteChanged but it didn't work also. As I remember MVVM Light Commands don't use CommandManager since some version. – Alex P. Aug 22 '13 at 15:55
  • then i dont know, sorry... I am not sure what the commandmanager has to do with MVVM Light, as the WPF system reevaluates the command – thumbmunkeys Aug 22 '13 at 15:57
1

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)

SvenG
  • 5,155
  • 2
  • 27
  • 36