RaiseCanExecuteChanged

RaiseCanExecuteChanged

我的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)));
        }
    }
}

然后在我的 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>

但是,当我按下“停止”按钮并且“状态”属性更改为“空闲”时,按钮仅将其文本更改为“开始”,但是直到我单击窗口中的某个位置之前,按钮一直保持禁用状态。

我将RaiseCanExecuteChanged添加到State属性的 setter 中,但没有帮助。

我究竟做错了什么?

另外,我不确定在同一命令中使用“开始/停止”和设置按钮文本使用“数据触发”的最佳方法,所以如果有人共享更好的方法,我将不胜感激。

最佳答案

您的问题不是RaiseCanExecuteChanged()不起作用,而是当UI依赖它时从后台线程修改State属性。

所以说如果我将您的代码相应地修改为:(这与.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);
}

现在,您可以看到Button从禁用切换回正确启用。

您可以从以下位置获取示例:Here

如果要坚持使用Thread,请将其切换为:
new Thread(
  () => {
    Thread.Sleep(5000);
    Application.Current.Dispatcher.BeginInvoke(
      new Action(() => State = WorkState.Idle));
  }).Start();

^^这现在可以再次正常工作,因为您无需从另一个线程修改属性。

10-06 05:25