Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass a variable as a CommandParameter

I'm trying to send a variable from the ViewModel as a parameter to a command. The command looks like this:

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

The ViewModel looks like this:

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

The xaml looks like this:

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

I've tried creating a property in the ViewModel instead of using the private local variable name, but that didnt work either. The object parameter always shows null in the call to CanExecute and the button is never enabled. If I change the CommandParameter value to Hello, then I receive Hello in the call to CanExecute, so I'm not sure why the variable doesnt work. Any help would be appreciated.

Update: I've also tried making a public property to the model (which I dont really want to expose the model, but just tried it to see if it works, but it doesnt).

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

And changed the xaml to this:

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

But still no luck. The ViewModel does implement INotifyPropertyChanged

like image 593
SwDevMan81 Avatar asked Sep 11 '12 13:09

SwDevMan81


People also ask

How do you use a CommandParameter?

Passing a parameter to the CanExecute and Execute methodsA parameter can be passed through the "CommandParameter" property. Once the button is clicked the selected address value is passed to the ICommand. Execute method. The CommandParameter is sent to both CanExecute and Execute events.

What is a command parameter?

The CommandParameter property is used to pass specific information to the command when it is executed. The type of the data is defined by the command. Many commands do not expect command parameters; for these commands, any command parameters passed will be ignored.

What is WPF CommandParameter?

CommandParameter - represents a user-defined data value that can be passed to the command when it is executed. CommandTarget - the object on which the command is being executed.


1 Answers

Is the CommandParameter always null or are you only checking the first time it is being executed?

It appears that the order in which you declare your properties matters in this case since setting the Command property causes the CanExecute to fire immediately before the CommandParameter has been set.

Try moving the CommandParameter property before the Command property:

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

Also, see here and here.

Edit

To ensure that your events are being raised properly you should raise the CanExecuteChanged event when the PersonModelProp value changes.

The Command:

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

And the view model:

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();
      }
    }
}
like image 136
Dylan Meador Avatar answered Oct 08 '22 00:10

Dylan Meador