1

当 CanExecute 条件发生变化时,我想 RaiseCanExecuteChanged 。例如:

public class ViewModel
{
public viewModel()
{
    Command = new RelayCommand(action,condition);
}

private  bool condition()
{
   return  this.Condition1&&this.Condition2&&this.Condition3;
}

public bool Condition1
{
  get{...}
  set{.... **command.RaiseCanExecuteChanged();}**
}
public bool Condition2
{
   get{...}
   set{.... command.**RaiseCanExecuteChanged();}**
}
public bool Condition3
{
  get{...}
  set{.... **command.RaiseCanExecuteChanged();}**
}

}

这很好用。但是我不喜欢写这么多的RaiseCanExecuteChanged,我想自动设置这些更改。例如,在 RelayCommand 中,创建一个名为 RaiseChanged 的​​新方法

  public void RaiseChanged(XXXXXX  XXX,  params string[] propertyNames)
  {
      // for each property in propertyNames,
     // RaiseCanExecuteChanged();
  }

我把 ViewModel vm 作为参数,并使用 vm.PropertyChanged+=(s,e)=>{} 但我认为这不是一个好方法。

有没有人有其他想法?

4

1 回答 1

0

我开发了我可以这样做的解决方案:

C# 视图模型:

public bool CanExecuteMethod(object sender)
      {
      }
public void ButtonExecuteMethod(object sender)
      {
      }

public event Action EventNotifyCanExecuteChanged;

private Action _DelegateNotifyCanExecuteChanged;
public Action DelegateNotifyCanExecuteChanged
     {
         get { return _DelegateNotifyCanExecuteChanged; }
         set { _DelegateNotifyCanExecuteChanged = value; }
     }

public void CanExecuteFlag
        {
   if (EventNotifyCanExecuteChanged != null) { EventNotifyCanExecuteChanged(); }
   if (_DelegateNotifyCanExecuteChanged != null) { _DelegateNotifyCanExecuteChanged();} 
        }

XAML:

< Button Content="Button Cmd-ExCeCh" HorizontalAlignment="Left" Margin="27,231,0,0"
         VerticalAlignment="Top" Width="120"
        Command="{mark:BindCommandResource MainWindowViewModel,
        ExecuteMethodName=ButtonExecuteMethod,
        CanExecuteMethodName=CanExecuteMethod,
        EventToInvokeCanExecuteChanged=EventNotifyCanExecuteChanged,
        PropertyActionCanExecuteChanged=DelegateNotifyCanExecuteChanged}"  />

我在我的开源项目 MVVM-WPF XAML Mark-up Binding Extensions - http://wpfmvvmbindingext.codeplex.com中放置/分享了这个解决方案

于 2013-05-18T18:13:52.160 回答