2015-04-29 6 views
2

Где я могу запустить мой метод CreateBestPriceListCanExecute для обновления моего CanExecute в приведенном ниже фрагменте кода?Обновление CanExecute из других действий ViewModel

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 Моего метод использует базу данных, и она имеет автоматическое обновление базы данных работают в фоновом режиме, что загружает информацию иногда. Мне нужно, чтобы мой CanExecute был ложным в это время, есть ли другой способ сделать это?

+0

У вас есть бесполезный тернарный оператор в методе 'CreateBestPriceListCanExecute'. –

ответ

1

Просто обновите CanExecute свойства из метода CreateBestPriceListCanExecute:

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

Тогда это просто до вас, как часто вы называете метод CreateBestPriceListCanExecute. Например, вы можете вызвать его из обработчика события DispatcherTimer.Tick.

Смежные вопросы