WPF MVVM命令canexecute启用/禁用按钮

我想在textbox属性文本不为null时启用RibbonButton。 当textbox属性文本为null时,禁用RibbonButton。 我想在ICommand中使用CanExecute方法。 我该怎么做?

视图:

  

视图模型

 class KomentarViewModel:BaseViewModel { #region Data private ICommand m_ButtonCommand; public ICommand ButtonCommand { get { return m_ButtonCommand; } set { m_ButtonCommand = value; } } private string textKomentar; public string TextKomentar { get { return this.textKomentar; } set { // Implement with property changed handling for INotifyPropertyChanged if (!string.Equals(this.textKomentar, value)) { textKomentar = value; OnPropertyChanged("TextKomentar"); } } } private ObservableCollection allCommentsInc; public ObservableCollection AllCommentsInc { get { return allCommentsInc; } set { allCommentsInc = value; OnPropertyChanged("AllCommentsInc"); } } public int idIncident { get; private set; } public Incident incident { get; private set; } #endregion #region Constructor public KomentarViewModel(int id) { CC_RK2Entities context = new CC_RK2Entities(); this.idIncident = id; AllCommentsInc = new ObservableCollection(context.Komentar.Where(a => a.Incident_id == idIncident)); incident = context.Incident.Where(a => a.id == idIncident).First(); //ButtonCommand = new RelayCommand(new Action(ShowMessage)); } #endregion #region Methods //ukaz napsany text public void ShowMessage(object obj) { //MessageBox.Show(obj.ToString()); MessageBox.Show(this.TextKomentar); } } 

RelayCommand

 namespace Admin.Shared.Commands { class RelayCommand : ICommand { private Action _action; public RelayCommand(Action action) { _action = action; } #region ICommand Members public bool CanExecute(object parameter) { return true; } public event EventHandler CanExecuteChanged; public void Execute(object parameter) { _action(parameter); } #endregion } } 

您需要像这样修改您的RelayCommand类

  class RelayCommand : ICommand { private Action _action; private Func _func; public RelayCommand(Action action,Func func) { _action = action; _func = func; } public void RaiseCanExecuteChanged() { if(CanExecuteChanged!=null) CanExecuteChanged(this,new EventArgs()); } #region ICommand Members public bool CanExecute(object parameter) { if (_func != null) return _func(); return true; } public event EventHandler CanExecuteChanged; public void Execute(object parameter) { _action(parameter); } #endregion } 

将ButtonCommand初始化为

 ButtonCommand = new RelayCommand((s) => ShowMessage(s),()=>!string.IsNullOrEmpty(TextKomentar)); 

从Text属性的setter中调用RaiseCanExcuteChanged

  public string TextKomentar { get { return this.textKomentar; } set { // Implement with property changed handling for INotifyPropertyChanged if (!string.Equals(this.textKomentar, value)) { textKomentar = value; OnPropertyChanged("TextKomentar"); } ButtonCommand.RaiseCanExecuteChanged(); } } 

为canexecute实现这个:

 public bool CanExecute(object parameter) {if(thistext available) return true; else return false; } 

因为,当ICommandCanExecute方法发生更改时,会引发CanExecute 。 当一些可能改变canexecute命令时,它会被调用。 并且可以执行更改应该更改为:

 public event EventHandler CanExecuteChanged { add { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested -= value; } } 

编辑

在您的视图模型构造函数中:

 m_ButtonCommand= new RelayCommand(Submit, CanSubmit); now method for this submit: private bool CanSubmit(object obj) { if(thistext available) return true; else return false; } public void Submit(object _) {//... code} public event EventHandler CanExecuteChanged { add { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested -= value; } } 

像这样做。

上次我使用Microsoft.Practices.Prism.Commands namesapce。 类DelegateCommand具有自己的RaiseCanExecuteChanged()方法。 所以好处是你不必编写自己的ICommand实现。

XAML:

     

视图模型:

 public class ViewModel { public DelegateCommand ExportCommand { get; } public ViewModel() { ExportCommand = new DelegateCommand(Export, CanDoExptor); } private void Export() { //logic } private bool _isCanDoExportChecked; public bool IsCanDoExportChecked { get { return _isCanDoExportChecked; } set { if (_isCanDoExportChecked == value) return; _isCanDoExportChecked = value; ExportCommand.RaiseCanExecuteChanged(); } } private bool CanDoExptor() { return IsCanDoExportChecked; } }