10

我正在尝试将 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);
           }
        }
     }
  }
}

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;
     }
  }
}

xaml 看起来像这样:

<Button Content="Edit" HorizontalAlignment="Right" Height="20" Width="80"
   Command="{Binding EditPersonCommand}" 
   CommandParameter="{Binding _PersonModel}" />

我尝试在 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");
  }
}

并将 xaml 更改为:

<Button Content="Edit" HorizontalAlignment="Right" Height="20"  Width="80"
   Command="{Binding EditPersonCommand}" 
   CommandParameter="{Binding PersonModelProp}" />

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

4

4 回答 4

18

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

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

尝试将 CommandParameter 属性移到 Command 属性之前:

<Button Content="Edit" HorizontalAlignment="Right" Height="20"  Width="80"
 CommandParameter="{Binding PersonModelProp}" 
 Command="{Binding EditPersonCommand}" />

另外,请参见此处此处

编辑

为确保您的事件被正确引发,您应该在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);
      }
  }
}

和视图模型:

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();
      }
    }
}
于 2012-09-11T14:21:47.173 回答
7

回答两点:

首先,正如@akton 提到的,您只能绑定到public properties。不过,它不一定DependencyProperty

其次,我花了一些时间才弄清楚,您必须在Command属性之前设置CommandParameter的绑定。 IE

<Button Content="Edit" HorizontalAlignment="Right" Height="20"  Width="80"
        CommandParameter="{Binding PersonModelProp}"
        Command="{Binding EditPersonCommand}" />

希望这可以帮助 :)

于 2012-09-11T14:22:48.757 回答
4

_PersonModel是私有的,因此无法访问。创建一个公开它的公共属性并绑定到CommandParameter. 请记住使该属性成为依赖属性(技术上不是必需的,但它有帮助)并且 ViewModel 应该实现 INotifyProperty changed 并触发 PropertyChanged 事件以便更新绑定。

于 2012-09-11T13:51:49.650 回答
3

我认为你的 EditPersonCommand 有问题(它没有被触发)。我用 relayCommand 检查它并且它工作!

这是代码:

视图模型:

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

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

        }

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

        public ICommand EditPersonCommand
        {
            get
            {
                if (_EditPersonCommand == null)
                {
                    _EditPersonCommand = new RelayCommand(ExecuteEditPerson,CanExecuteEditPerson);
                }
                return _EditPersonCommand;
            }
        }


        private bool CanExecuteEditPerson(object parameter)
        {
            PersonModel p = parameter as PersonModel;

            return (p != null) && (p.Age > 0);
        }


        private void ExecuteEditPerson(object o)
        {

        }


        public PersonModel PersonModelProp
        {
            get
            {
                return _PersonModel;
            }
            set
            {
                _PersonModel = value;
                NotifyPropertyChanged("PersonModelProp");
            }
        }


    }

还有这个 RelayCommand(火灾事件好!)

      public class RelayCommand : ICommand
        {
            #region Constants and Fields


            private readonly Predicate<object> canExecute;


            private readonly Action<object> execute;

            #endregion

            #region Constructors and Destructors


            public RelayCommand(Action<object> execute)
                : this(execute, null)
            {
            }

            public RelayCommand(Action<object> execute, Predicate<object> canExecute)
            {
                if (execute == null)
                {
                    throw new ArgumentNullException("execute");
                }

                this.execute = execute;
                this.canExecute = canExecute;
            }

            #endregion

            #region Events


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

                remove
                {
                    CommandManager.RequerySuggested -= value;
                }
            }

            #endregion

            #region Implemented Interfaces

            #region ICommand


            [DebuggerStepThrough]
            public bool CanExecute(object parameter)
            {
                return this.canExecute == null || this.canExecute(parameter);
            }

            public void Execute(object parameter)
            {
                this.execute(parameter);
            }

            #endregion

            #endregion
        }

圣诞节:

<Button Content="Edit" HorizontalAlignment="Right" Height="20"  Width="80"
 CommandParameter="{Binding PersonModelProp}" 
 Command="{Binding EditPersonCommand}" />
于 2012-09-11T14:46:43.947 回答