如何将变量作为CommandParameter传递

SwD*_*n81 8 c# wpf command mvvm commandparameter

我正在尝试将ViewModel中的变量作为参数发送到命令.该命令如下所示:

public class EditPersonCommand : ICommand
{
  private bool _CanExecute = false;

  public bool CanExecute(object parameter)
  {
     PersonModel p = parameter as PersonModel;
     CanExecuteProperty = (p != null) && (p.Age > 0);
     return CanExecuteProperty;
  }

  public event EventHandler CanExecuteChanged;

  public void Execute(object parameter) { }

  private bool CanExecuteProperty
  {
     get { return _CanExecute; }
     set
     {
        if (_CanExecute != value)
        {
           _CanExecute = value;
           EventHandler can_execute = CanExecuteChanged;
           if (can_execute != null)
           {
              can_execute.Invoke(this, EventArgs.Empty);
           }
        }
     }
  }
}
Run Code Online (Sandbox Code Playgroud)

ViewModel看起来像这样:

public class PersonViewModel : ViewModelBase
{
  private PersonModel _PersonModel;
  private EditPersonCommand _EditPersonCommand;

  ///<remarks>
  /// must use the parameterless constructor to satisfy <Window.Resources>
  ///</remarks>
  public PersonViewModel()
     : this(new PersonModel())
  {

  }

  public PersonViewModel(PersonModel personModel)
  {
     _PersonModel = personModel;
  }

  public ICommand EditPersonCommand
  {
     get
     {
        if (_EditPersonCommand == null)
        {
           _EditPersonCommand = new EditPersonCommand();
        }
        return _EditPersonCommand;
     }
  }
}
Run Code Online (Sandbox Code Playgroud)

xaml看起来像这样:

<Button Content="Edit" HorizontalAlignment="Right" Height="20" Width="80"
   Command="{Binding EditPersonCommand}" 
   CommandParameter="{Binding _PersonModel}" />
Run Code Online (Sandbox Code Playgroud)

我尝试在ViewModel中创建一个属性,而不是使用私有本地变量名,但这也没有用.将object parameter始终显示null在调用CanExecute和按钮从未启用.如果我将CommandParameter值更改为Hello,那么我Hello在调用中收到CanExecute,所以我不确定为什么变量不起作用.任何帮助,将不胜感激.

更新: 我也尝试为模型创建一个公共属性(我真的不想公开模型,但只是尝试了它是否有效,但它没有).

// Added this to the ViewModel
public PersonModel PersonModelProp
{
  get
  {
     return _PersonModel;
  }
  set
  {
     _PersonModel = value;
     OnPropertyChanged("PersonModelProp");
  }
}
Run Code Online (Sandbox Code Playgroud)

并将xaml更改为:

<Button Content="Edit" HorizontalAlignment="Right" Height="20"  Width="80"
   Command="{Binding EditPersonCommand}" 
   CommandParameter="{Binding PersonModelProp}" />
Run Code Online (Sandbox Code Playgroud)

但仍然没有运气.ViewModel确实实现了INotifyPropertyChanged

Dyl*_*dor 14

CommandParameter是否始终为null或者您是仅在第一次执行时检查?

在这种情况下,您声明属性的顺序似乎很重要,因为设置Command属性会导致CanExecute在设置CommandParame之前立即触发.

尝试在Command属性之前移动CommandParameter属性:

<Button Content="Edit" HorizontalAlignment="Right" Height="20"  Width="80"
 CommandParameter="{Binding PersonModelProp}" 
 Command="{Binding EditPersonCommand}" />
Run Code Online (Sandbox Code Playgroud)

另外,请看这里这里.

编辑

为确保正确引发事件,应在PersonModelProp值更改时引发CanExecuteChanged事件.

命令:

public class EditPersonCommand : ICommand
{
  public bool CanExecute(object parameter)
  {
     PersonModel p = parameter as PersonModel;
     return p != null && p.Age > 0;
  }

  public event EventHandler CanExecuteChanged;

  public void Execute(object parameter) 
  {
      //command implementation
  }

  public void RaiseCanExecuteChanged()
  {
      var handler = CanExecuteChanged;
      if(handler != null)
      {
          handler(this, EventArgs.Empty);
      }
  }
}
Run Code Online (Sandbox Code Playgroud)

和视图模型:

public class PersonViewModel : ViewModelBase
{
  private PersonModel _PersonModel;
  private EditPersonCommand _EditPersonCommand;

  ///<remarks>
  /// must use the parameterless constructor to satisfy <Window.Resources>
  ///</remarks>
  public PersonViewModel()
     : this(new PersonModel())
  {
      _EditPersonCommand = new EditPersonCommand();
  }

  public PersonViewModel(PersonModel personModel)
  {
     _PersonModel = personModel;
  }

  public ICommand EditPersonCommand
  {
     get
     {
         return _EditPersonCommand;
     }
  }

  public PersonModel PersonModelProp
  {
      get
      {
         return _PersonModel;
      }
      set
      {
         _PersonModel = value;
         OnPropertyChanged("PersonModelProp");
         EditPersonCommand.RaiseCanExecuteChanged();
      }
    }
}
Run Code Online (Sandbox Code Playgroud)


Abd*_*men 7

两点答案:

首先,正如@akton所提到的,您只能绑定到公共属性.但它不一定DependencyProperty.

其次,这让我想象一下,你必须在Command属性之前设置CommandParameter 的绑定.即

<Button Content="Edit" HorizontalAlignment="Right" Height="20"  Width="80"
        CommandParameter="{Binding PersonModelProp}"
        Command="{Binding EditPersonCommand}" />
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助 :)