从另一个 ViewModel 的动作更新 CanExecute

Update CanExecute from another ViewModel's actions

我在哪里 运行 我的 CreateBestPriceListCanExecute 方法来更新下面代码片段中的 CanExecute

class BestPriceViewModel : ViewModelBase
{
    ObservableCollection<BestPriceModel> bestPriceList = new ObservableCollection<BestPriceModel>();

    public ICommand createBestPriceListCommand;
    private bool canExecute = true;
    public BestPriceViewModel()
    {
        createBestPriceListCommand = new RelayCommand(CreateBestPriceList, param => this.CanExecute);
        btnLoadItemList = "Import";
    }     public ObservableCollection<BestPriceModel> BestPriceList
    {
        get { return this.bestPriceList; }
        set
        {
            if (this.bestPriceList != value)
            {
                this.bestPriceList = value;
                this.OnPropertyChanged("BestPriceList");
            }
        }
    }

    public bool CanExecute
    {
        get
        {
            return this.canExecute;
        }

        set
        {
            if (this.canExecute == value)
            {
                return;
            }

            this.canExecute = value;
        }
    }
    public ICommand CreateBestPriceListCommand
    {
        get
        {
            return createBestPriceListCommand;
        }
        set
        {
            createBestPriceListCommand = value;
        }
    }

    public bool CreateBestPriceListCanExecute()
    {
        bool DbCheck = DataBaseAccess.connection.State != ConnectionState.Open &&
        DataBaseAccess.connection.State != ConnectionState.Fetching &&
        DataBaseAccess.connection.State != ConnectionState.Executing ? true : false;
        return DbCheck;
    }
}

我的 CreateBestPrice 方法正在使用数据库,它在后台有一个自动数据库更新程序 运行ning,有时会上传信息。那时我需要我的 CanExecute 为假,还有其他方法吗?

只需从您的 CreateBestPriceListCanExecute 方法更新您的 CanExecute 属性:

public void CreateBestPriceListCanExecute()
{
    CanExecute = DataBaseAccess.connection.State != ConnectionState.Open &&
    DataBaseAccess.connection.State != ConnectionState.Fetching &&
    DataBaseAccess.connection.State != ConnectionState.Executing ? true : false;
}

那么调用 CreateBestPriceListCanExecute 方法的频率就完全取决于您了。例如,您可以从 DispatcherTimer.Tick 事件处理程序中调用它。